Slide 1

Slide 1 text

A functional view to Rust

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

For attending Still here feel free to interrupt @flaper87 [email protected]

Slide 4

Slide 4 text

What’s up with this Rust-lang thingy ?

Slide 5

Slide 5 text

Does it really buy me something?

Slide 6

Slide 6 text

What does it have to do with functional languages?

Slide 7

Slide 7 text

Boring Stuff ~ and @ sigils are gone

Slide 8

Slide 8 text

Boring Stuff ~ and @ sigils are gone Immutable by default

Slide 9

Slide 9 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii

Slide 10

Slide 10 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii almost everything is an expression

Slide 11

Slide 11 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii almost everything is an expression static with local inference

Slide 12

Slide 12 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii almost everything is an expression static with local inference linear Types

Slide 13

Slide 13 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii almost everything is an expression static with local inference linear Types ...

Slide 14

Slide 14 text

Boring Stuff ~ and @ sigils are gone Immutable by default raii almost everything is an expression static with local inference linear Types ... ...

Slide 15

Slide 15 text

Memory Safety

Slide 16

Slide 16 text

passing by-ref “lends” the value fn main() { let mut x = 5; add_one(&mut x); println!("{}", x); } fn add_one(num: &mut i32) { *num += 1; }

Slide 17

Slide 17 text

fn main() { let x = Box::new(5); add_one(x); println!("{}", x); // error } fn add_one(mut num: Box) { *num += 1; } passing by-val “gives” the value

Slide 18

Slide 18 text

fn main() { let x = 5; add_one(x); println!("{}", x); // No error } fn add_one(num:i32) -> i32 { num + 1 } wait, you said no-ref moves the value

Slide 19

Slide 19 text

Type & Trait system

Slide 20

Slide 20 text

Bounded Polymorphism

Slide 21

Slide 21 text

Bounds T: Send T: Sync T: Sync+Copy T: U

Slide 22

Slide 22 text

struct MyType {...}

Slide 23

Slide 23 text

struct MyType {...}

Slide 24

Slide 24 text

fn send {...}

Slide 25

Slide 25 text

Lifetimes

Slide 26

Slide 26 text

struct Foo<'a> { x: &'a i32, } fn main() { let x; // -+ x goes into scope // | { // | let y = &5; // ---+ y goes into scope let f = Foo { x: y }; // ---+ f goes into scope x = &f.x; // | | error here } // ---+ f and y go out of scope // | println!("{}", x); // | } lifetimes, regions, scope…

Slide 27

Slide 27 text

Mutability

Slide 28

Slide 28 text

Is mutability limited to linearity? fn main() { let mut x = 5; add_one(&mut x); println!("{}", x); } fn add_one(num: &mut i32) { *num += 1; }

Slide 29

Slide 29 text

Inherited Mutability fn main() { let x = MyStruct {a: 5}; let mut z = x; // z.a is mutable println!("{}", x); }

Slide 30

Slide 30 text

Internal Mutability fn main() { let x = Cell(5); x.set(6); }

Slide 31

Slide 31 text

Soundness

Slide 32

Slide 32 text

struct Foo<'a> { x: &'a mut i32, // ERROR: &mut T is not copyable } impl<’a> Copy for Foo {} fn is_copy() {} fn main() { is_copy::(); } unaliased mutable references

Slide 33

Slide 33 text

struct Foo<'a> { x: &'a i32, } impl<’a> Copy for Foo {} fn is_copy() {} fn main() { is_copy::(); }

Slide 34

Slide 34 text

Enough with the type system

Slide 35

Slide 35 text

Pattern Matching

Slide 36

Slide 36 text

Pattern Matching enum OptInt { Value(i32), Missing, // Warning on unused variant } match OptInt::Value(5) { OptInt::Value(a) if a > 5 => println!("{}", a), OptInt::Value(a) => println!("Got an int: {}!", a), OptInt::Missing => println!("No such luck."), }

Slide 37

Slide 37 text

Pattern Matching let x = 5; match x { i @ 1 … 5 => println!("got range item: {}!", i), _ => println!("anything!") }

Slide 38

Slide 38 text

Iterators

Slide 39

Slide 39 text

Iterator trait: fold let sum = (1..4).fold(0, |sum, x| sum + x);

Slide 40

Slide 40 text

Iterator trait: filter for i in (1..100).filter(|&x| x % 2 == 0) { println!("{}", i); }

Slide 41

Slide 41 text

Iterator trait: map for i in (1..100).map(|x| x + 1) { println!("{}", i); }

Slide 42

Slide 42 text

Thread, Mutex, Channel

Slide 43

Slide 43 text

Channels use std::sync::{Arc, Mutex}; use std::thread; use std::sync::mpsc; fn main() { let data = Arc::new(Mutex::new(0u32)); let (tx, rx) = mpsc::channel(); for _ in 0..10 { let (data, tx) = (data.clone(), tx.clone()); thread::spawn(move || { let mut data = data.lock().unwrap(); *data += 1; tx.send(()); }); } for _ in 0..10 { rx.recv(); } } Multi-producer Single-consumer

Slide 44

Slide 44 text

Questions?