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

Intro to Rust

Intro to Rust

Slides from my Rust talk at WeRise.tech

Dan Callahan

June 24, 2017
Tweet

More Decks by Dan Callahan

Other Decks in Technology

Transcript

  1. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } ➟ Stack Heap x PTR LEN CAP a b c 3 3 Try it
  2. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } ➟ a Stack Heap x PTR LEN CAP a b c 3 3 Try it
  3. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } ➟ Stack Heap x PTR LEN CAP a b c 3 3 Owner Try it
  4. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } ➟ Stack Heap x PTR LEN CAP a b c 3 3 x Owner Try it
  5. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } Stack Heap x PTR LEN CAP a b c 3 3 x Owner ➟ a Try it
  6. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } Stack Heap Try it
  7. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap Try it
  8. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap &Vec<Char> Try it
  9. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap &Vec<Char> y Try it
  10. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y x PTR LEN CAP a b c 3 3 x Owner Try it
  11. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y x PTR LEN CAP a b c 3 3 x Owner Try it
  12. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y x PTR LEN CAP a b c 3 3 x Owner a Try it
  13. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y Owner Try it
  14. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y Owner ? Try it
  15. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y Owner Compiler
 X Try it
  16. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } Stack Heap Owner Try it
  17. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap Owner Try it
  18. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap Vec<Char> Owner Try it
  19. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap Vec<Char> y PTR LEN CAP Owner Try it
  20. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP x Try it
  21. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP x Try it
  22. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP 3 3 x Try it
  23. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP 3 3 x Try it
  24. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP 3 3 x y Try it
  25. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP 3 3 x y Try it
  26. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 x PTR LEN CAP 3 3 x y Compiler
 X Try it
  27. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; // println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 y Try it
  28. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; // println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y PTR LEN CAP Owner a b c 3 3 y a Try it
  29. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; // println!(“{}”, x[0]); } println!(“{}”, y[0]); } Stack Heap Owner Try it
  30. One unique owner per allocation Memory freed when owner leaves

    scope
 All other references must also be out of scope Ownership can be passed
 Invalidates previous owner Ownership Rules
  31. If it compiles, it won’t segfault If it compiles, there

    are no data races No runtime overhead Rules are enforced by the compiler
  32. Try it fn main() { let x = 5; x

    += 1; println!("{}", x) }
  33. Try it fn main() { let x = 5; x

    += 1; println!("{}", x) } error[E0384]: re-assignment of immutable variable `x`
  34. Try it fn main() { let mut x = 5;

    x += 1; println!("{}", x) }
  35. Try it fn main() { let mut x = 5;

    x += 1; println!("{}", x) } 6
  36. Try it fn main() { let mut x = 5;

    add_one(&mut x); println!("{}", x) } fn add_one(num: &mut i32) { *num += 1; }
  37. Try it fn main() { let mut x = 5;

    add_one(&mut x); println!("{}", x) } fn add_one(num: &mut i32) { *num += 1; } 6
  38. Try it fn main() { let mut x = 5;

    let y = &mut x; add_one(y); println!("{}", x) } fn add_one(num: &mut i32) { *num += 1; }
  39. Try it fn main() { let mut x = 5;

    let y = &mut x; add_one(y); println!("{}", x) } fn add_one(num: &mut i32) { *num += 1; } error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
  40. Try it fn main() { let mut x = 5;

    { let y = &mut x; add_one(y); } println!("{}", x) } fn add_one(num: &mut i32) { *num += 1;
  41. Try it fn main() { let mut x = 5;

    { let y = &mut x; add_one(y); } println!("{}", x) } fn add_one(num: &mut i32) { *num += 1; 6
  42. All variables are immutable by default Only one mutable reference

    at a time
 But as many immutable &’s as you want Mutable references block all other access
 The &mut must go out of scope before using other &’s Mutability Rules
  43. Python + Rust Interop
 github.com/callahad/python-rust-ffi Servo Parallel Browser Project
 servo.org

    WebAssembly
 (Soon: github.com/rust-lang/rust/issues/38805) Demos