Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust

Sibi
October 10, 2020

 Rust

Sibi

October 10, 2020
Tweet

More Decks by Sibi

Other Decks in Programming

Transcript

  1. Rust
    Rust
    Sibi Prabakaran
    ILUGC, Oct 10, 2020

    View Slide

  2. Agenda
    Agenda
    My experience with Rust
    Introduc on to Rust
    Why Rust

    View Slide

  3. My experience
    My experience
    Embedded programming
    STM32F3 Microcontroller
    Some work in my day job
    h ps:/
    /crates.io/crates/credstash

    View Slide

  4. Rust
    Rust

    View Slide

  5. Heap and Stack
    Heap and Stack
    Heap: Memory set aside for dynamic alloca on.
    Stack: Memory set aside for a thread.

    View Slide

  6. Ownership rules
    Ownership rules
    Each value in Rust has a variable that’s called its
    owner.
    There can only be one owner at a me.
    When the owner goes out of scope, the value will
    be dropped.
    {
    let s1 = String::from("hello ilugc"); // s1 is valid from
    this point forward
    // do stuff with s1

    View Slide

  7. } // this scope is now over,
    and s1 is no
    // longer valid

    View Slide

  8. Data interaction
    Data interaction
    fn main() {
    let x: i32 = 5;
    let y = x;
    println!("Output: {} {}", x, y);
    }
    fn main() {
    let x = String::from("hello ilugc");
    let y = x;
    println!("Output: {} {}", x, y);
    }

    View Slide

  9. Ownership
    Ownership
    fn main() {
    let s = String::from("hello"); // s comes into scope
    takes_ownership(s); // s's value moves into the
    function...
    // ... and so is no longer
    valid here
    let x = 5; // x comes into scope
    makes_copy(x); // x would move into the
    function,
    // but i32 is Copy, so it’s
    okay to still
    // use x afterward

    View Slide

  10. References
    References
    fn main() {
    let s1 = String::from("hello");
    let len = calculate_length(&s1);
    println!("The length of '{}' is {}.", s1, len);
    }
    fn calculate_length(s: &String) -> usize {
    s.len()
    }

    View Slide

  11. References (2)
    References (2)
    fn main() {
    let mut s = String::from("hello");
    change(&mut s);
    }
    fn change(some_string: &mut String) {
    some_string.push_str(", world");
    }

    View Slide

  12. Snippet 1
    Snippet 1
    fn main() {
    let mut x = String::from("ilugc");
    let r1 = &mut x;
    let r2 = &mut x;
    println!("{}", r1);
    }

    View Slide

  13. Snippet 2
    Snippet 2
    fn main() {
    let mut x = String::from("ilugc");
    let r1 = &x;
    let r2 = &mut x;
    println!("{}", r1);
    }

    View Slide

  14. Snippet 3
    Snippet 3
    use std::thread;
    fn main() {
    let v = vec![1, 2, 3];
    let handle = thread::spawn(|| {
    println!("Here's a vector: {:?}", v);
    });
    handle.join().unwrap();
    }

    View Slide

  15. Hard parts
    Hard parts
    Learning curve
    Embedded ecosystem not mature yet.
    Scien fic libraries.

    View Slide

  16. Why Rust
    Why Rust
    Compile me guarantees
    Zero cost abstrac on
    No excep ons. So easy FFI integra on.
    Error messages
    Community

    View Slide

  17. Learning
    Learning
    resources
    resources
    Rust book
    OReily’s book
    Embedded book

    View Slide

  18. Questions
    Questions

    View Slide