Slide 1

Slide 1 text

In Rust We Trust Alex Burkhart - @saterus

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Rust = [Haskell, C++, Python].max()

Slide 4

Slide 4 text

"The goal is to design and implement a safe, concurrent, practical, static systems language." — Rust Project FAQ

Slide 5

Slide 5 text

Gradually Replace C++

Slide 6

Slide 6 text

"Rust keeps the C abstract machine model but innovates on the language interface." — Raphael Poss, Rust for Functional Programmers

Slide 7

Slide 7 text

No Runtime

Slide 8

Slide 8 text

No Garbage Collector

Slide 9

Slide 9 text

Zero Cost Abstractions

Slide 10

Slide 10 text

Better Programmer Interface

Slide 11

Slide 11 text

Immutability by Default fn main() { let x = 0; x = 1; // => error: // re-assignment of immutable variable `x` let mut y = -10; y = 20; // => ok! }

Slide 12

Slide 12 text

Type Inference fn double(n: u32) -> u32 { n * n } fn main() { let x = 500; let y = double(250); let z = x + y; }

Slide 13

Slide 13 text

Enums enum Coffee { Hot(u8), // temp F Iced(bool), // still has ice Instant, } fn main() { let first_cup: Coffee = Coffee::Iced(true); let second_cup: Coffee = Coffee::Hot(212); println!("Drink {:?} then {:?}.", first_cup, second_cup); }

Slide 14

Slide 14 text

Destructuring fn drinkable(cup: Coffee) -> bool { match cup { Coffee::Hot(120...150) => true, Coffee::Iced(x) => x, _ => false, } }

Slide 15

Slide 15 text

Traits impl Eq for String { /* details omitted */ } impl Hash for String { /* details omitted */ } impl MyHashMap where K: Eq + Hash { // details omitted } fn main() { let map: MyHashMap = MyHashMap::new(); // compiles! } // note: not quite the std lib implementations...

Slide 16

Slide 16 text

Iterators & Closures fn main() { let squares = (0..10).map(|n| n * n); for i in squares { println!("{:?}", i); } }

Slide 17

Slide 17 text

Hygenic Macros fn main() { let v1 = vec![1,2,3]; // vec![] expands into: let mut v2 = Vec::with_capacity(3); v2.push(1); v2.push(2); v2.push(3); }

Slide 18

Slide 18 text

Testing /// double will do the obvious. ex: /// ``` /// assert_eq!(double(2), 4); /// ``` fn double(n: u32) -> u32 { n * 2 } #[test] fn double_zero_is_zero() { assert_eq!(0, double(0)); }

Slide 19

Slide 19 text

Module System // in project_a/src/traffic/color.rs mod traffic { pub enum Color { Red, Yellow, Green }; } // in project_b/src/main.rs extern crate project_a; use traffic::Color; fn main() { let stoplight = Color::Red; println!("Imported a {:?} stoplight!", stoplight); }

Slide 20

Slide 20 text

Package Management

Slide 21

Slide 21 text

Remarkable Error Messages fn main() { let coffee = Box::new(Coffee::Hot(85)); let dupped = coffee.clone(); } :18:25: 18:32 error: type `Box` does not implement any method in scope named `clone` :18 let dupped = coffee.clone(); ^~~~~~~ :18:25: 18:32 help: methods from traits can only be called if the trait is implemented and in scope; the following trait defines a method `clone`, perhaps you need to implement it: :18:25: 18:32 help: candidate #1: `core::clone::Clone`

Slide 22

Slide 22 text

Wonderful Community

Slide 23

Slide 23 text

What is in Rust?

Slide 24

Slide 24 text

What is Not in Rust?

Slide 25

Slide 25 text

No Null

Slide 26

Slide 26 text

No Implicit Type Conversion

Slide 27

Slide 27 text

No Exceptions

Slide 28

Slide 28 text

No Inheritance

Slide 29

Slide 29 text

No Function Overloading

Slide 30

Slide 30 text

No Laziness

Slide 31

Slide 31 text

No Higher Kinded Types

Slide 32

Slide 32 text

No Strict Purity

Slide 33

Slide 33 text

No Garbage Collector

Slide 34

Slide 34 text

No Manual Memory Management

Slide 35

Slide 35 text

Manual Memory Management • Pointer Arithmetic • Null Pointers • Double Frees • Never Frees • Dangling Pointers

Slide 36

Slide 36 text

Unsafety = Memory Unsafety • Accessing Uninitialized Data • Writing Invalid Data • Breaking Aliasing Rules • Data Races • Calling Foreign Functions

Slide 37

Slide 37 text

Human Still Required • Rc Cycles -> Leaks • Deadlocks • I/O • Int Overflow

Slide 38

Slide 38 text

Memory Management Stategies

Slide 39

Slide 39 text

Manual Memory Management

Slide 40

Slide 40 text

Garbage Collection

Slide 41

Slide 41 text

Automatic Reference Counting

Slide 42

Slide 42 text

Ownership

Slide 43

Slide 43 text

Ownership Rules Summary 1. Single Owner 2. Mutability Requires Exclusivity 3. Sharing Requires Immutability

Slide 44

Slide 44 text

Single Responsible Owner fn main() { // allocate some memory let stoplight = Color::Red; // access value of stoplight println!("the value of stoplight: {:?}", stoplight); // owner `stoplight` falls out of scope // owner drops its property }

Slide 45

Slide 45 text

Ownership is a Tree fn main() { let mut drink_caddie = Vec::new(); for _ in 0..4 { drink_caddie.push(Coffee::Hot(212)); } println!("all the coffee: {:?}", drink_caddie); // owner `coffee` falls out of scope // owner drops its property // drop the Vec --> drop each Coffee --> drop the u8 }

Slide 46

Slide 46 text

Mutability fn main() { let mut coffee = Box::new(Coffee::Hot(85)); *coffee = Coffee::Hot(212); println!("the value of coffee: {:?}", coffee); }

Slide 47

Slide 47 text

Ownership Transfer fn main() { let coffee_shop = Box::new(Coffee::Hot(85)); let customer = coffee_shop; println!("the value of customer: {:?}", customer); println!("the value of coffee_shop: {:?}", coffee_shop); // => error! use of moved value `coffee_shop`! }

Slide 48

Slide 48 text

Borrowing fn main() { let showing = BluRay::Disc; let friend_a = &showing; let friend_b = &showing; println!("hooray! everyone can share! {:?}", showing); println!("hooray! everyone can share! {:?}", friend_a); println!("hooray! everyone can share! {:?}", friend_b); }

Slide 49

Slide 49 text

Aliasing & Mutability: Pick One fn reheat(cup: &mut Coffee) { /* snip */ } fn main() { let mut coffee = Box::new(Coffee::Hot(85)); reheat(&mut coffee); println!("coffee temp: {:?}", coffee); // => coffee temp: Hot(180) }

Slide 50

Slide 50 text

Safe Abstractions • Vec • LinkedList • Rc • Arc

Slide 51

Slide 51 text

Concurrency Building Blocks

Slide 52

Slide 52 text

Data Race 1. 2+ threads accessing the same data 2. at least 1 is unsynchronized 3. at least 1 is writing

Slide 53

Slide 53 text

Shared Nothing fn main() { let (tx, rx) = channel(); for task_num in 0..8 { let tx = tx.clone(); Thread::spawn(move || { let msg = format!("Task {:?} done!", task_num); tx.send(msg).unwrap(); }).detach(); } for data in rx.iter() { println!("{:?}", data); } }

Slide 54

Slide 54 text

Shared Immutable Memory fn main() { let (tx, rx) = channel(); let huge_struct = HugeStruct::new(); let arc = Arc::new(huge_struct); for task_num in 0..8 { let tx = tx.clone(); let arc = arc.clone(); Thread::spawn(move || { let msg = format!("Task {:?}: Accessed {:?}", task_num, arc.huge_name); tx.send(msg).unwrap(); }).detach(); } for data in rx.iter() { println!("{:?}", data); // 10x => Task N: Accessed I'M HUGE } }

Slide 55

Slide 55 text

Mutation with Synchronization fn main() { let (tx, rx) = channel(); let huge_struct = HugeStruct::new(); let arc = Arc::new(Mutex::new(huge_struct)); for task_num in 0..8 { let tx = tx.clone(); let arc = arc.clone(); Thread::spawn(move || { let mut guard = arc.lock().unwrap(); guard.access_count += 1; let msg = format!("Task {:?}: Accessed Count {:?}", task_num, guard.access_count); tx.send(msg).unwrap(); // drop the arc -> drop the guard -> unlock the mutex }).detach(); } for data in rx.iter() { println!("{:?}", data); // 10x => Task N: Accessed Count: M } }

Slide 56

Slide 56 text

When to Use Rust?

Slide 57

Slide 57 text

CPU Bound

Slide 58

Slide 58 text

I/O Bound

Slide 59

Slide 59 text

Low Latency

Slide 60

Slide 60 text

Memory Constrained

Slide 61

Slide 61 text

Interoperative

Slide 62

Slide 62 text

Requires Portability

Slide 63

Slide 63 text

High Security

Slide 64

Slide 64 text

High Reliability

Slide 65

Slide 65 text

Great Software

Slide 66

Slide 66 text

Big News: 1.0

Slide 67

Slide 67 text

Rust 1.0

Slide 68

Slide 68 text

Rust at LambdaConf An Introduction to Rust: Or, "Who Got Types in My Systems Programming!" Jared Roesch 11am-1pm Sunday

Slide 69

Slide 69 text

Columbus Rust Society @columbusrust

Slide 70

Slide 70 text

Mutually Human mutuallyhuman.com

Slide 71

Slide 71 text

Thanks! Alex Burkhart @saterus