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

Rust TechSpeakers Workshop Intro

Dan Callahan
September 16, 2018

Rust TechSpeakers Workshop Intro

An introduction to Rust covering mutability, borrowing, and ownership (how Rust manages memory without a mandatory garbage collector or reference counting).

Dan Callahan

September 16, 2018
Tweet

More Decks by Dan Callahan

Other Decks in Programming

Transcript

  1. Three Channels:
 Nightly → Beta → Stable Releases every six

    weeks (approx.) Releases “Stability Without Stagnation”
  2. Three Channels:
 Nightly → Beta → Stable Releases every six

    weeks (approx.) Experimental Features:
 Only available on Nightly
 Require explicit opt-in Releases “Stability Without Stagnation”
  3. New edition every 2-3 years (approx.)
 Call attention to cumulative

    improvements since prior edition
 Opportunity to change Rust itself (new keywords, semantics, etc.) Editions
  4. New edition every 2-3 years (approx.)
 Call attention to cumulative

    improvements since prior edition
 Opportunity to change Rust itself (new keywords, semantics, etc.) Editions are opt-in
 Default is always Rust 2015 Editions
  5. New edition every 2-3 years (approx.)
 Call attention to cumulative

    improvements since prior edition
 Opportunity to change Rust itself (new keywords, semantics, etc.) Editions are opt-in
 Default is always Rust 2015 Compiler will always support all previous editions
 Rust 2015 code can depend on Rust 2018 code, and vice versa Editions
  6. Rustup — Install and manage Rust itself Cargo — Main

    build, test, etc. tool for Rust code Tooling
  7. Rustup — Install and manage Rust itself Cargo — Main

    build, test, etc. tool for Rust code Clippy — Comprehensive code linting tool Tooling
  8. Rustup — Install and manage Rust itself Cargo — Main

    build, test, etc. tool for Rust code Clippy — Comprehensive code linting tool Rustfmt — Automatic code formatting Tooling
  9. Rustup — Install and manage Rust itself Cargo — Main

    build, test, etc. tool for Rust code Clippy — Comprehensive code linting tool Rustfmt — Automatic code formatting RLS — Language Server Protocol implementation for Rust Tooling
  10. Rustup — Install and manage Rust itself Cargo — Main

    build, test, etc. tool for Rust code Clippy — Comprehensive code linting tool Rustfmt — Automatic code formatting RLS — Language Server Protocol implementation for Rust Tooling Works great with Visual Studio Code!
  11. Try it fn main() { let x = 5; x

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

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

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

    x += 1; println!("{}", x) } 6
  15. #[derive(Debug)] struct Point { x: i32, y: i32, } fn

    main() { let mut point = Point { x: 0, y: 0 }; println!("Current value: {:?}", &point); let foo = &mut point; foo.x = 1; foo.y = 2; // What happens if you move this up a few lines, between foo.x and foo.y? println!("Current value: {:?}", &point); }
  16. Only one mutable borrow at a time
 But as many

    immutable &’s as you want Mutable borrows block all other access
 The &mut must end before using other &’s Borrowing Rules
  17. fn main() { { let x = vec![‘a’,‘b’,‘c’]; println!(“{}”, x[0]);

    } } ➟ Stack Heap x PTR LEN CAP a b c 3 3 Try it
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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 Free x’s memory here
  23. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

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

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

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap &Vec<Char> Try it
  26. 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
  27. 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
  28. 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
  29. 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
  30. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

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

    y = &x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap y Owner ? Try it
  32. 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
  33. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

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

    y = x; println!(“{}”, x[0]); } println!(“{}”, y[0]); } ➟ Stack Heap Owner Try it
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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 Free y’s memory here
  48. fn main() { let y; { let x = vec![‘a’,‘b’,‘c’];

    y = x; // println!(“{}”, x[0]); } println!(“{}”, y[0]); } Stack Heap Owner Try it
  49. 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
  50. If it compiles, it won’t segfault If it compiles, there

    are no data races No runtime overhead
 No implicit refcounts, no garbage collector, etc. Rules are enforced by the compiler