Slide 1

Slide 1 text

Concurrency in Rust is Boring (and that's good)

Slide 2

Slide 2 text

Software Engineer at Tuenti MadRust co-organizer  jrvidal  _rvidal

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Concurrency: the End is Near

Slide 5

Slide 5 text

Rust is a Systems Programming Language Ring 3 Ring 2 Ring 1 Ring 0 Kernel Device drivers Applications Device drivers Least privileged Most privileged Chrislb, CC BY-SA 3.0. Hertzsprung, CC BY-SA 3.0. Virtual Machines ‐ Ravi, Nair. SimonWaldherr, CC BY-SA 4.0

Slide 6

Slide 6 text

...and more fn dot_product( vec1: &[u32], vec2: &[u32] ) -> u32 { vec1 .iter() .zip(vec2) .map(|(a, b)| a * b) .sum() }

Slide 7

Slide 7 text

Concurrency in Rust is boring use std::{thread, sync}; let arc = sync::Arc::new("foo"); let mutex = sync::Mutex::new(1); thread::spawn(|| { println!("Look ma, concurrency") }); let (sender, receiver) = sync::mpsc::channel();

Slide 8

Slide 8 text

Concurrency in Rust is boring (luckily) use std::{thread, sync}; let arc = sync::Arc::new("foo"); let mutex = sync::Mutex::new(1); thread::spawn(|| { println!("Look ma, concurrency") }); let (sender, receiver) = sync::mpsc::channel(); NO DATA RACES

Slide 9

Slide 9 text

Ownership and Borrowing Single owner let x = Box::new(7); consume(x); println!("x = {}", x);

Slide 10

Slide 10 text

Ownership and Borrowing Single owner let x = Box::new(7); consume(x); // ⚠ borrow of moved value: `x` println!("x = {}", x);

Slide 11

Slide 11 text

Ownership and Borrowing Borrowing prevents moves let x = Box::new(7); let pointer = &x; consume(x); println!("pointer = {}", pointer);

Slide 12

Slide 12 text

Ownership and Borrowing Borrowing prevents moves let x = Box::new(7); let pointer = &x; // ⚠ cannot move out of `x` // because it is borrowed consume(x); println!("pointer = {}", pointer);

Slide 13

Slide 13 text

Ownership and Borrowing Multiple read-only borrows or single mutable borrow let mut x = 7; let pointer = &x; let another_pointer = &mut x; *another_pointer += 1; println!("pointer = {}", pointer);

Slide 14

Slide 14 text

Ownership and Borrowing Multiple read-only borrows or single mutable borrow let mut x = 7; let pointer = &x; // ⚠ cannot borrow `x` as mutable // because it is also borrowed as immutable let another_pointer = &mut x; *another_pointer += 1; println!("pointer = {}", pointer);

Slide 15

Slide 15 text

Ownership and Borrowing and Concurrency let mut x = 1; let pointer = &mut x; thread::spawn(|| { *pointer = 7; }); thread::spawn(|| { println!("x = {}", x); });

Slide 16

Slide 16 text

Ownership and Borrowing and Concurrency let mut x = 1; let pointer = &mut x; thread::spawn(|| { *pointer = 7; }); // ⚠ cannot borrow `x` as immutable // because it is also borrowed as mutable thread::spawn(|| { println!("x = {}", x); });

Slide 17

Slide 17 text

Send and Sync For everything else, the type system. let original = Rc::new(1); let copy = original.clone(); thread::spawn(|| { println!("original = {}", original) }); let copy2 = copy.clone();

Slide 18

Slide 18 text

Send and Sync For everything else, the type system. let original = Rc::new(1); let copy = original.clone(); // ⚠ `Rc` cannot be shared // between threads safely thread::spawn(|| { println!("original = {}", original) }); let copy2 = copy.clone(); pub fn spawn(f: F) where F: FnOnce + Send

Slide 19

Slide 19 text

Userland tour

Slide 20

Slide 20 text

Userland tour rayon: data parallelism crossbeam: concurrent data structures lazy_static: lazy-initialized static values parking_lot: better impls of std primitives actix: Actors threadpool: basic threadpool

Slide 21

Slide 21 text

What about the future?

Slide 22

Slide 22 text

What about the future? async fn get_user(uid: u32) -> Option { let db_user = db.get_user(uid).await; let metadata = external_service .get_metadata(uid).await; db_user.map(|u| User::from(u, metadata)) }

Slide 23

Slide 23 text

What about the future? Withoutblogs

Slide 24

Slide 24 text

1st class documentation Error messages cargo & crates.io Enums for error handling Servo WebAssembly Serde Zero-cost closures Community Redox libcore and embedded Editions, release channels Macros: declarative and procedural Tempus fugit https://www.meetup.com/MadRust/ https://www.rust-lang.org/learn

Slide 25

Slide 25 text

Concurrency, Do Not Fear