Slide 1

Slide 1 text

1 Hack without fear! Hack without fear!

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

3 What Rust has to offer Strong safety guarantees… No seg-faults, no data-races, expressive type system. …without compromising on performance. No garbage collector, no runtime. Goal: Confident, productive systems programming

Slide 4

Slide 4 text

4 Two years old on May 15! •••

Slide 5

Slide 5 text

5 Photo credit: FrontOffice https://commons.wikimedia.org/wiki/File:VIE_Office_Park.JPG

Slide 6

Slide 6 text

6 Photo credit: Artaxerxes https://commons.wikimedia.org/wiki/File:Grindstone_Cafe_in_Lyndonville_Vermont.jpg

Slide 7

Slide 7 text

7 Photo credit: Artaxerxes https://commons.wikimedia.org/wiki/File:Grindstone_Cafe_in_Lyndonville_Vermont.jpg

Slide 8

Slide 8 text

8 Photo credit: Artaxerxes https://commons.wikimedia.org/wiki/File:Grindstone_Cafe_in_Lyndonville_Vermont.jpg

Slide 9

Slide 9 text

9 six months later…

Slide 10

Slide 10 text

10 struct Event { } key: u32, name: InternedString, count: u32, Photo credit: Artaxerxes https://commons.wikimedia.org/wiki/File:Grindstone_Cafe_in_Lyndonville_Vermont.jpg

Slide 11

Slide 11 text

11 Photo credit: Gratisography https://www.pexels.com/photo/nature-walking-animal-strong-4075/ struct Event { } key: u32, name: InternedString, count: u32, struct InternedString { } value: Rc, “reference-counted string” non-atomically reference-counted string

Slide 12

Slide 12 text

12 struct Event { } key: u32, name: InternedString, count: u32, Photo credit: Artaxerxes https://commons.wikimedia.org/wiki/File:Grindstone_Cafe_in_Lyndonville_Vermont.jpg error: the type `InternedString` does not implement `Send`

Slide 13

Slide 13 text

13 Zero-cost abstractions fn is_whitespace(text: &str) -> bool { text.chars() .all(|c| c.is_whitespace()) } fn load_images(paths: &[PathBuf]) -> Vec { paths.par_iter() .map(|path| Image::load(path)) .collect() }

Slide 14

Slide 14 text

14 Zero-cost abstractions Memory safety & data-race freedom + = Confident, productive systems programming

Slide 15

Slide 15 text

15 Rust the language Memory safety Traits Parallelism Unsafe Rust the community Rust in Firefox Community processes

Slide 16

Slide 16 text

16 Photo credit: Amelia Wells https://www.flickr.com/photos/speculummundi/6424242877/ Memory safety

Slide 17

Slide 17 text

17 Zero-cost abstractions Memory safety & data-race freedom + = Confident, productive systems programming

Slide 18

Slide 18 text

Zero-cost abstractions void example() { vector vector; … auto& elem = vector[0]; … } string [0] … elem vector data length capacity [0] [n] […] … ‘H’ … ‘e’ Stack and inline layout. Lightweight references Deterministic destruction Stack Heap C++

Slide 19

Slide 19 text

void example() { vector vector; … auto& elem = vector[0]; vector.push_back(some_string); cout << elem; } vector data length capacity [0] … [0] [1] elem Aliasing: more than one pointer to same memory. Dangling pointer: pointer to freed memory. Mutating the vector freed old contents. C++ Memory safety

Slide 20

Slide 20 text

~ Ownership and Borrowing ~ Type Ownership Alias? Mutate? T Owned ✓

Slide 21

Slide 21 text

Ownership n. The act, state, or right of possessing something. 21

Slide 22

Slide 22 text

fn main() { let mut book = Vec::new(); book.push(…); book.push(…); publish(book); publish(book); } fn publish(book: Vec) { … } Ownership Take ownership of the vector 22 Error: use of moved value: `book` String book data length capacity [0] [1] data length capacity ~~~~~~~~~~~ Give ownership.

Slide 23

Slide 23 text

23 Moves compared with copy constructors:

Slide 24

Slide 24 text

void main() { vector book(); book.push_back(…); book.push_back(…); publish(book); publish(book); } void publish(book: vector) { … } Owns the vector 24 string book data length capacity [0] [1] data length capacity [0] [1] C++ Invoke copy ctor

Slide 25

Slide 25 text

fn main() { let mut book = Vec::new(); book.push(…); book.push(…); publish(book.clone()); publish(book); } fn publish(book: Vec) { … } 25 string book data length capacity [0] [1] [0] [1]

Slide 26

Slide 26 text

26 Moves compared with copy constructors: Moves compared with rvalue references: …in Rust, `clone()` is explicit.

Slide 27

Slide 27 text

data length capacity data length capacity void main() { vector book(); book.push_back(…); book.push_back(…); publish(std::move(book)); publish(book); } void publish(book: vector) { … } 27 C++ string book data length capacity [0] [1]

Slide 28

Slide 28 text

28 …in Rust, enforced at compilation time. Moves compared with copy constructors: Moves compared with rvalue references: …in Rust, `clone()` is explicit.

Slide 29

Slide 29 text

Borrow v. To receive something with the promise of returning it. 29

Slide 30

Slide 30 text

~ Ownership and Borrowing ~ Type Ownership T Alias? Mutate? Owned ✓ &T Shared reference ✓

Slide 31

Slide 31 text

fn main() { let mut book = Vec::new(); book.push(…); book.push(…); publish(&book); publish(&book); } fn publish(book: &Vec) { … } Shared borrow Change type to a reference to a vector 31 String book data length capacity [0] [1] Borrow the vector, creating a reference book

Slide 32

Slide 32 text

cannot mutate while shared 32 “Don’t break your friend’s toys” let mut book = Vec::new(); book.push(…); { let r = &book; book.push(…); r.push(…); } book.push(…); book mutable here ~~~~~~~~~~~ book borrowed here ~~~~~~~~~ * Actually: mutation only under controlled circumstances * now book is mutable again r goes out of scope; borrow ends

Slide 33

Slide 33 text

33 fn example() { let mut vector = Vec::new(); … let elem = &vector[0]; vector.push(some_string); … } vector borrowed here cannot mutate here Revisit initial example ~~~~~~~~~~~~~~~~~~~~~

Slide 34

Slide 34 text

~ Ownership and Borrowing ~ Type Ownership T &T Alias? Mutate? Owned Shared reference ✓ ✓ &mut T Mutable reference ✓

Slide 35

Slide 35 text

fn main() { let mut book = Vec::new(); book.push(…); book.push(…); edit(&mut book); edit(&mut book); } fn edit(book: &mut Vec) { book.push(…); } Mutable borrow Mutable reference to a vector 35 String book data length capacity [0] [1] book [2] Mutable borrow [3]

Slide 36

Slide 36 text

cannot access while borrowed but &mut ref can mutate 36 “No, it’s my turn now!” let mut book = Vec::new(); book.push(…); { let r = &mut book; book.len(); r.push(…); } book.push(…); book mutable here ~~~~~~~~~ book borrowed here borrow ended; accessible again r goes out of scope; borrow ends

Slide 37

Slide 37 text

‘l 37 Lifetime of a reference let mut book = Vec::new(); … { let r: &String = &book[0]; … } let r: &String = &book; let r: &’l String = &book;

Slide 38

Slide 38 text

38 fn first<‘a>(v: &’a Vec) -> &’a String { return &v[0]; } “Returns a reference derived from `v`” fn first(v: &Vec) -> &String { return &v[0]; } (more common shorthand)

Slide 39

Slide 39 text

39 fn example() { let mut book = Vec::new(); { let r = first(&book); book.push(…); } } ~~~~~~~~~~~ book borrowed for this span Tracking borrows across functions fn first<‘a>(v: &’a Vec) -> &’a String { return &v[0]; }

Slide 40

Slide 40 text

40 Traits Photo credit: Andy Leppard https://www.flickr.com/photos/creativecomputer/261445720/

Slide 41

Slide 41 text

41 trait Clone { fn clone(&self) -> Self; } Implemented for a given type Method that borrows its receiver impl Clone for Vec { fn clone(&self) -> Vec { let mut v = Vec::new(); for elem in self { v.push(elem.clone()); } return v; } } Implementation for vectors Create new vector Iterate (using references)… …push clone of each element. Return `v`

Slide 42

Slide 42 text

42 // “Safe to send between threads” trait Send { } // “Safe to memcpy” trait Copy { } String u32 Rc u32 f32 String Rc Marker traits ✅ Arc

Slide 43

Slide 43 text

43 Parallelism Photo credit: Dave Gingrich https://www.flickr.com/photos/ndanger/2744507570/

Slide 44

Slide 44 text

Library-based concurrency 44 Originally: Rust had message passing built into the language. Now: library-based, multi-paradigm. Libraries leverage ownership and traits to avoid data races.

Slide 45

Slide 45 text

Data races Sharing Mutation No ordering Data race Sound familiar? 45

Slide 46

Slide 46 text

fn qsort(vec: &mut [i32]) { if vec.len() <= 1 { return; } let pivot = vec[random(vec.len())]; let mid = vec.partition(vec, pivot); let (less, greater) = vec.split_at_mut(mid); rayon::join(|| qsort(less), || qsort(greater)); } [0] [1] [2] [3] […] [n] vec: &mut [i32] less: &mut [i32] greater: &mut [i32] `&mut` can safely go between threads — because it is unaliased! fn split_at_mut<‘a>(v: &’a mut [T], …) -> (&’a mut [T], &’a mut [T])

Slide 47

Slide 47 text

~ Concurrency paradigms ~ Paradigm Fork-join Ownership? Borrowing? ✓ Message Passing Locking ✓ ✓ ✓ Lock-free Futures … ✓ ✓ ✓ ✓

Slide 48

Slide 48 text

48 Unsafe

Slide 49

Slide 49 text

Safe abstractions unsafe { … } Ownership/borrowing/traits give tools to enforce safe abstraction boundaries. Trust me. fn split_at_mut(…) { } Validates input, etc. 49

Slide 50

Slide 50 text

50 Rust compiler (~350KLOC) has approx. 4% unsafe code, much of which is in the LLVM bindings.

Slide 51

Slide 51 text

51

Slide 52

Slide 52 text

52 First foray: mp4 demuxer https://is.gd/idNDQw

Slide 53

Slide 53 text

53 Stylo and WebRender Photo credit: Fenring https://commons.wikimedia.org/wiki/File:CSS-shade.svg (Other browsers: 0-10 FPS)

Slide 54

Slide 54 text

54 Community Photo credit: David McSpadden https://www.flickr.com/photos/familyclan/15535822737/

Slide 55

Slide 55 text

55 From http://jvns.ca/blog/2016/09/11/rustconf-keynote/ Open and welcoming

Slide 56

Slide 56 text

Our responsibility [after 1.0] is to ensure that you never dread upgrading Rust. Since the early days of Rust, there have only been two things you could count on: safety, and change. And sometimes not the first one. Rust 1.0: Stability as a deliverable

Slide 57

Slide 57 text

RFC Nightly Beta Stable ~ The feature pipeline ~

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

RFC Nightly Beta Stable Unstable features are available { ~ The feature pipeline ~

Slide 60

Slide 60 text

~ The feature pipeline ~ RFC Nightly Beta Stable 6 week release cycle; only stable features {

Slide 61

Slide 61 text

Interested in Rust? 61 intorust.com users.rust-lang.org rust-lang.github.io/book ❤ #rust-beginners on irc.mozilla.org Also, stickers!