Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


hpr3426 :: Rust 101: Episode 0 - What in Tarnishing?

BlacKernel teaches you what rust is and how it is different from Python or C.

<< First, < Previous, Latest >>

Hosted by BlacKernel on 2021-09-20 is flagged as Clean and is released under a CC-BY-SA license.
Tags: rust, programming, raii, python, c.
Listen in ogg, spx, or mp3 format. | Comments (3)

Part of the series: Programming 101

A series focusing on concepts and the basics of programming

Talking Points

  • What is Rust?
    • " Garbage Collection " - Resource Acquisition Is Initialization (RAII)
    • Strict Typing with Type Inference
    • Reference pointers
    • Immutable by default
    • Unsafe Mode
  • Why use Rust over Python?
    • Speed
    • Compiled
      1. Help from compiler
      2. Smaller binary size
      3. Useful in high throughput/embedded applications
    • Logically consistent
  • Why use Rust over C?
    • Safe by default
    • Easier to read
    • Forces you to write good code
    • Arrays without stupidity++ and built in vectors
    • Option<T> and Result<T> or a match {} made in heaven

Show Notes

Strict Typing

fn main() {

    // Type declared with var: <T> syntax
    let penguin_one: &str = "gentoo";
    
    // Type &str is inherited from "gentoo"
    let penguin_two = "gentoo";
    
    // Will not panic if they are the same
    assert_eq!(penguin_one, penguin_two);
}

Reference Pointers

Wrong Way:

fn print_u8_vector(vec: Vec<u8>) {
    println!("{:?}", vec);
}

fn main() {
    let penguin_ages: Vec<u8> = vec!(2, 4, 6);
    print_u8_vector(penguin_ages);
    
    // This line will throw an error
    println!("{}", penguin_ages[0]);
}

Correct Way:

fn print_u8_vector(vec: &Vec<u8>) {
    println!("{:?}", vec);
}

fn main() {
    let penguin_ages: Vec<u8> = vec!(2, 4, 6);
    print_u8_vector(&penguin_ages);
    
    // This line will print '2'
    println!("{}", penguin_ages[0]);
}

Immutable By Default

Wrong Way:

fn main() {
    let my_num = 2;
    
    // This line will throw an error
    my_num = my_num + 1;
    println!("{}", my_num);
}

Correct Way:

fn main() {
    let mut my_num = 2;
    my_num = my_num + 1;
    
    // This line will print '3'
    println!("{}", my_num);
}

Unsafe Code

Hello World Program in C in Rust:

extern "C" {
    fn printf(input: &str);
}

fn main() {
    unsafe {
        printf("Hello, World!");
    }
}

Contact Me

Email: izzyleibowitz at pm dot me

Mastodon: at blackernel at nixnet dot social


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2021-09-20T16:01:09Z by Trey

Thank you.

I have been considering learning some Rust, and this has given me the nudge needed to give it a try.

Looking forward to "Hello World" episode.

Comment #2 posted on 2021-09-20T19:38:44Z by Hipstre

Rust 101, Episode 0

Great to hear you talk about languages the way that you do. You give a lot of context without a lot of lingo. Looking forward to the next episode.

Lisp: Everything is a list.
Unix: Everything is a file.
Ruby: Everything is an object.
Haskell: Everything is a function.
Rust: EVERYTHING IS AN ERROR!

Comment #3 posted on 2021-10-04T20:56:17Z by Honkeymagoo

another fun way to learn rust

Another fun way to learn rust
https://doc.rust-lang.org/stable/rust-by-example/

<< First, < Previous, Latest >>

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the P in HPR stand for ?