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

A Rusty Guide to Types and Safety

A Rusty Guide to Types and Safety

Slides for the Leeds Digital Festival Talk.

https://github.com/crispthinking/rust-workshop

Will Speak

April 25, 2018
Tweet

More Decks by Will Speak

Other Decks in Programming

Transcript

  1. Will Speak - Developer
    A Rusty Guide to
    Types and Safety
    or
    Let’s Rewrite it
    in Rust!

    View full-size slide

  2. Complete Online Safety for

    Social Brands, Digital Platforms,
    Advertisers and Kids
    Through world-leading AI and a global team of digital risk
    experts we deliver complete Trust and Safety for social
    brands, digital platforms, advertisers and kids from bad
    actors that exploit, extort, distress, offend and misinform.

    View full-size slide

  3. Will Speak - Developer
    A Rusty Guide to
    Types and Safety
    or
    Let’s Rewrite it
    in Rust!

    View full-size slide

  4. @willspeak
    iwillspeak
    Will Speak

    View full-size slide

  5. We’re Hiring!

    View full-size slide

  6. Rust What is rust?
    Types How types can help you
    Concurrency Fearless parallel
    Advanced Unsafe code and more

    View full-size slide

  7. What is Rust?

    View full-size slide

  8. Systems
    Programming
    Language

    View full-size slide

  9. Safe,
    Concurent,
    Fast

    View full-size slide

  10. Safe,
    Concurent,
    Fast
    Pick
    Three

    View full-size slide



  11. // Hello world, in Rust!
    fn main() {
    println!("Hello, world!");
    }
    Hello Rust

    View full-size slide

  12. let book = Book::new("Excession");
    println!("My book: {}", book);
    Ownership

    View full-size slide

  13. let my_book = Book::new("Excession");
    let your_book = my_book;
    println!("Your book: {}", your_book);
    // !!ERROR - vv This won't work vv.
    println!("My book: {}", my_book);
    Ownership

    View full-size slide

  14. error[E0382]: use of moved value: `my_book`
    --> src/main.rs:5:31
    |
    3 | let your_book = my_book;
    | --------- value moved here
    4 | println!("Your book: {:?}", your_book);
    5 | println!("My book: {:?}", my_book); // !!ERROR - This won't work.
    | ^^^^^^^ value used here after move
    |
    Ownership

    View full-size slide

  15. let book = Book::new("Matter");
    // !!ERROR - `book` isn't mutable
    book.next_page();
    Mutation

    View full-size slide

  16. error[E0596]: cannot borrow immutable local variable `book` as
    mutable
    --> src/main.rs:3:5
    |
    2 | let book = Book::new("Matter");
    | ---- consider changing this to `mut book`
    3 | book.next_page(); // !!ERROR - `book` isn't mutable
    | ^^^^ cannot borrow mutably
    Mutation

    View full-size slide

  17. let mut book = Book::new("Matter");
    // OK now, book is mutable
    book.next_page();
    Mutation

    View full-size slide

  18. Borrows
    are
    Pointers

    View full-size slide

  19. Borrows
    are
    References

    View full-size slide

  20. let my_book = Book::new("Inversions");
    let my_borrow = &my_book;
    println!("Original book: {:?}", my_book);
    println!("Borrowed book: {:?}", my_borrow);
    Borrowing

    View full-size slide

  21. let my_borrow: &Book;
    {
    let my_book = Book::new("Inversions");
    my_borrow = &my_book;
    }
    println!("Borrowed book: {:?}", my_borrow);
    Borrowing

    View full-size slide

  22. error[E0597]: `my_book` does not live long enough
    --> src/main.rs:5:22
    |
    5 | my_borrow = &my_book;
    | ^^^^^^^ borrowed value does not live long enough
    6 | }
    | - `my_book` dropped here while still borrowed
    7 | println!("Borrowed book: {:?}", my_borrow);
    8 | }
    | - borrowed value needs to live until here
    Borrowing

    View full-size slide

  23. let mut my_book = Book::new(“Inversions");
    let my_borrow: &mut Book = &my_book;
    my_book.next_page();
    Borrowing

    View full-size slide

  24. We’ve got
    types!

    View full-size slide

  25. Integers i32 i64 u8 u32 …
    Strings String &str
    Booleans bool
    Arrays & Slices [u32; 6] &[i64]
    Tuples (i8, String, bool)

    View full-size slide

  26. struct Book {
    title: String,
    page: i32,
    }
    Structs

    View full-size slide

  27. enum BookOperation {
    Inspect,
    Return(Book),
    Lend(String),
    }
    Enums

    View full-size slide


  28. match operation {
    Return(book) => println!(
    "Returning {}", book.title),
    Lend(title) => println!("Lending {}", title),
    _ => ()
    }
    Enums

    View full-size slide

  29. enum Option {
    Some(T),
    None,
    }
    Null?

    View full-size slide

  30. pub struct Person {
    forename: String,
    surname: Option,
    }
    Null?

    View full-size slide

  31. fn print_surname(person: Person) {
    if let Some(surname) = person.surname {
    println!("{}'s surname is {}", person.forename,
    surname);
    } else {
    println!("{} has no surname");
    }
    }
    Null?

    View full-size slide

  32. enum Result {
    Ok(T),
    Err(E),
    }
    Null?

    View full-size slide

  33. pub fn hello_to_file(path: &Path) -> Result<(), Error> {
    let mut file = File::create(&path)?;
    write!(file, "hello world")?;
    Ok(())
    }
    Try?

    View full-size slide

  34. Further
    Typing

    View full-size slide

  35. render_to_path("hello/world/", true);
    Further Typing

    View full-size slide

  36. render_to_path("hello/world/", PathMode::IgnoreMissing);
    render_to_path("hello/world/", PathMode::Create);
    Further Typing

    View full-size slide

  37. Fearless
    Concurrency

    View full-size slide

  38. let mutex = Mutex::new(10);
    Mutex

    View full-size slide

  39. let lock = mutex.lock();
    match lock {
    Ok(mut guard) => {
    *guard += 1;
    println!("Value: {0}", guard);
    }
    Err(_) => println!("Mutex poisoned")
    }
    Mutex

    View full-size slide

  40. fn sum_of_squares(input: &[i32]) -> i32 {
    input.iter()
    .map(|&i| i * i)
    .sum()
    }
    Rayon

    View full-size slide

  41. use rayon::prelude::*;
    fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- just change that!
    .map(|&i| i * i)
    .sum()
    }
    Rayon

    View full-size slide

  42. Fireflowers
    &
    Unsafe

    View full-size slide

  43. struct ListNode {
    data: T,
    next: &'a ListNode,
    prev: &'a ListNode,
    }
    The Linked List

    View full-size slide

  44. struct ListNode {
    data: T,
    next: *mut ListNode,
    prev: *mut ListNode,
    }
    Breaking Out

    View full-size slide

  45. crates.io
    Rust is More

    View full-size slide

  46. Any questions?

    View full-size slide

  47. Exercises
    https://github.com/crispthinking/rust-workshop

    View full-size slide