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

Rust: Systems Programming for All!

Rust: Systems Programming for All!

A talk given at General Assembly on 2017-06-06. Covers the value of Rust, with a focus on ownership and borrowing.

nikomatsakis

June 06, 2017
Tweet

More Decks by nikomatsakis

Other Decks in Programming

Transcript

  1. 2 What is Rust all about? Elegant code that runs

    fast. Code that does the right thing.
  2. 4

  3. JavaScript / Ruby / Python / Java / C# /

    … 6 Virtual Machine or Runtime Native library C / C++
  4. 8 Algorithm “Learn All The Things”: Find shelf S with

    computer books. Let N = 1. While less than N books on shelf S { Borrow the Nth book from shelf S. Read it and return it. N = N + 1. }
  5. [0] 12 void example() { vector<string> shelf; shelf.push_back(…); for (auto&

    book : shelf) { read(book); shelf.push_back(…); } } capacity length data [1] [2] [3] book shelf
  6. 13 Crash, read random data, wev C/C++ Java Throw exception

    JavaScript Do… something Python Something… slightly different Rust? Detect problem when compiling Iterator invalidation: modify while iterating ==> Runtime Error
  7. 14 foo.rs <machine code> <input> <output> “compilation” “execution” Rust: check

    ownership + borrowing Others: something goes wrong here
  8. “not mutable” owns [0] 15 capacity length data [1] [2]

    1. Every value is owned by a single variable. owns moved ~~~~~~~~~~~~~~~~ (a) Owner gets to decide whether it is mutable. ✅ ❌ ❌ “moved” ~~~~~~~~~~~~~~~~ fn example() { let mut shelf = Vec::new(); shelf.push(…); let shelf2 = shelf; shelf.push(…); print(shelf2.len()); shelf2.push(…); }
  9. borrows owns [0] 16 capacity length data [1] [2] `shelf`

    borrowed here book ~~~~~~~~~~~~~~~~ 2. Shared borrows make the value temporarily immutable. fn example() { let mut shelf = Vec::new(); shelf.push(…); for book in &shelf { read(book); shelf.push(…); } shelf.push(…); } ❌ “immutable while borrowed” ✅
  10. “mutably borrowed” owns [0] 17 capacity length data [1] [2]

    `shelf` mutably borrowed here book ~~~~~~~~~~~~~~~~~~~~~ 3. Mutable borrows have unique access to the value. borrows mutably ❌ ✅ ✅ make changes fn example() { let mut shelf = Vec::new(); shelf.push(…); for book in &mut shelf { edit(book); print(shelf.len()); } shelf.push(…); }
  11. 18 1. Every value is owned by a single variable.

    2. Shared borrows make the value temporarily immutable. 3. Mutable borrows have unique access to the value. shelf shelf book shelf book
  12. 19 Recap: Mutation while iterating C/C++ Maximally efficient Maximally dangerous

    Others Less efficient Risky ⛈⛈ Rust Maximally efficient Perfectly safe
  13. 20 “Must be this tall to write multi-threaded code” Idea

    credit: David Baron Mozilla Engineer Extraordinaire and All-Around Great Guy
  14. 21 Shared state and threads Message-passing Data parallelism C and

    C++, Java, C#, … C and C++, Java, C#, … Erlang, Go, JavaScript, … Rust Rust Rust Futures and Async I/O JavaScript, C#, … Rust
  15. 23 let book = channel.receive(); read(book); 1. Every value is

    owned by a single variable. ❌ “moved” moved let book = …; channel.send(book); ~~~~~~~~~~~~
  16. 25 fn load_images(paths: &[PathBuf]) -> Vec<Image> { paths.iter() .map(|path| {

    Image::load(path) }) .collect() } For each path… …load an image… …create and return a vector. paths = [ “a.jpg”, “b.png”, …, “c.jpg” ] borrowed from caller
  17. 26 fn load_images(paths: &[PathBuf]) -> Vec<Image> { paths.par_iter() .map(|path| {

    Image::load(path) }) .collect() } Make it parallel paths = [ “a.jpg”, “b.png”, …, “c.jpg” ] extern crate rayon; Third-party library
  18. 27 fn load_images(paths: &[PathBuf]) -> Vec<Image> { let mut jpgs

    = 0; paths.par_iter() .map(|path| { if path.ends_with(“.jpg”) { jpgs += 1; } Image::load(path) }) .collect() } How many jpgs seen so far? …add 1 to the counter. If current file name ends in “jpg”… 0 0 + 1 0 + 1 1 1 1
  19. 28 borrows mutably 1. Every value is owned by a

    single variable. 3. Mutable borrows have unique access to the value. fn load_images(paths: &[PathBuf]) -> Vec<Image> { let mut jpgs = 0; paths.par_iter() .map(|path| { if path.ends_with(“.jpg”) { jpgs += 1; } Image::load(path) }) .collect() }
  20. fn load_images(paths: &[PathBuf]) -> Vec<Image> { let mut jpgs =

    0; paths.par_iter() .map( ) .collect() } 29 borrows mutably borrows mutably |path| { if path.ends_with(“.jpg”) { jpgs += 1; } Image::load(path) } |path| { if path.ends_with(“.jpg”) { jpgs += 1; } Image::load(path) } 3. Mutable borrows have unique access to the value. ~~~~~~~~~ ❌ ~~~~~~~~~ ❌
  21. 31 More examples… ( Foo vs Option<Foo> ) No null

    pointers Robust error handling Generic programming …
  22. fn pass_safely(…) { if !car_coming() { } } Safe abstractions

    unsafe { drive_in_left_lane(); } Trust me. Validates input, etc. 34
  23. 36

  24. 38 https://www.meetup.com/BostonRust/
 Next meetup: June 28 in Cambridge http://boston-rust.herokuapp.com/ Boston

    area Worldwide rust-lang.org
 irc.mozilla.org, #rust, #rust-beginners doc.rust-lang.org/nightly/book/second-edition/ intorust.com ❤ A lso, stickers!