Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A functional view to Rust

A functional view to Rust

flaper87

March 27, 2015
Tweet

More Decks by flaper87

Other Decks in Technology

Transcript

  1. A functional view
    to Rust

    View Slide

  2. View Slide

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

    View Slide

  4. What’s up with this Rust-lang
    thingy ?

    View Slide

  5. Does it really buy me
    something?

    View Slide

  6. What does it have to do with
    functional languages?

    View Slide

  7. Boring Stuff
    ~ and @
    sigils are
    gone

    View Slide

  8. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. Memory Safety

    View Slide

  16. 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;
    }

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. Type & Trait system

    View Slide

  20. Bounded
    Polymorphism

    View Slide

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

    View Slide

  22. struct MyType {...}

    View Slide

  23. struct MyType {...}

    View Slide

  24. fn send {...}

    View Slide

  25. Lifetimes

    View Slide

  26. 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…

    View Slide

  27. Mutability

    View Slide

  28. 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;
    }

    View Slide

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

    View Slide

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

    View Slide

  31. Soundness

    View Slide

  32. 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

    View Slide

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

    View Slide

  34. Enough with the type system

    View Slide

  35. Pattern
    Matching

    View Slide

  36. 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."),
    }

    View Slide

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

    View Slide

  38. Iterators

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  42. Thread, Mutex,
    Channel

    View Slide

  43. 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

    View Slide

  44. Questions?

    View Slide