1
Systems Programming for All!
Systems Programming for All!
Slide 2
Slide 2 text
2
What is Rust all about?
Elegant code that runs fast.
Code that does the right thing.
Slide 3
Slide 3 text
3
Two years old on May 15!
•••
Slide 4
Slide 4 text
4
Slide 5
Slide 5 text
5
Photo credit: tao lin
https://www.flickr.com/photos/taolin/8639700222/
Slide 6
Slide 6 text
JavaScript / Ruby / Python / Java / C# / …
6
Virtual Machine or Runtime Native library
C / C++
Slide 7
Slide 7 text
7
Images used without permission.
Slide 8
Slide 8 text
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.
}
Slide 9
Slide 9 text
9
Images used without permission.
Slide 10
Slide 10 text
10
Images used without permission.
Slide 11
Slide 11 text
11
C / C++
Slide 12
Slide 12 text
[0]
12
void example() {
vector shelf;
shelf.push_back(…);
for (auto& book : shelf) {
read(book);
shelf.push_back(…);
}
}
capacity
length
data
[1]
[2]
[3]
book
shelf
Slide 13
Slide 13 text
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
“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(…);
}
Slide 16
Slide 16 text
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”
✅
Slide 17
Slide 17 text
“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(…);
}
Slide 18
Slide 18 text
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
Slide 19
Slide 19 text
19
Recap: Mutation while iterating
C/C++ Maximally efficient
Maximally dangerous
Others Less efficient
Risky ⛈⛈
Rust Maximally efficient
Perfectly safe
Slide 20
Slide 20 text
20
“Must be this tall to
write multi-threaded
code”
Idea credit: David Baron
Mozilla Engineer Extraordinaire and All-Around Great Guy
Slide 21
Slide 21 text
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
Slide 22
Slide 22 text
22
Message-passing
Slide 23
Slide 23 text
23
let book = channel.receive();
read(book);
1. Every value is
owned by a
single variable.
❌ “moved”
moved
let book = …;
channel.send(book);
~~~~~~~~~~~~
Slide 24
Slide 24 text
24
Data parallelism
Slide 25
Slide 25 text
25
fn load_images(paths: &[PathBuf]) -> Vec {
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
27
fn load_images(paths: &[PathBuf]) -> Vec {
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
Slide 28
Slide 28 text
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 {
let mut jpgs = 0;
paths.par_iter()
.map(|path| {
if path.ends_with(“.jpg”) { jpgs += 1; }
Image::load(path)
})
.collect()
}
Slide 29
Slide 29 text
fn load_images(paths: &[PathBuf]) -> Vec {
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.
~~~~~~~~~
❌
~~~~~~~~~
❌
31
More examples…
( Foo vs Option )
No null pointers
Robust error handling
Generic programming
…
Slide 32
Slide 32 text
32
Unsafe
Slide 33
Slide 33 text
Rust
33
Ownership and Borrowing Unsafe Code
Raw Computer
Slide 34
Slide 34 text
fn pass_safely(…) {
if !car_coming() {
}
}
Safe abstractions
unsafe {
drive_in_left_lane();
}
Trust me.
Validates input, etc.
34
Slide 35
Slide 35 text
35
Community
Photo credit: David McSpadden
https://www.flickr.com/photos/familyclan/15535822737/
Slide 36
Slide 36 text
36
Slide 37
Slide 37 text
37
From http://jvns.ca/blog/2016/09/11/rustconf-keynote/
Open and welcoming
Slide 38
Slide 38 text
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!