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 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 Slide

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

    View Slide

  4. @willspeak
    iwillspeak
    Will Speak

    View Slide

  5. We’re Hiring!

    View Slide

  6. Rust!

    View Slide

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

    View Slide

  8. What is Rust?

    View Slide

  9. Systems
    Programming
    Language

    View Slide

  10. Safe,
    Concurent,
    Fast

    View Slide

  11. Safe,
    Concurent,
    Fast
    Pick
    Three

    View Slide

  12. Safety

    View Slide

  13. Hello Rust!

    View Slide



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

    View Slide

  15. Ownership

    View Slide

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

    View Slide

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

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

  19. Mutation

    View Slide

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

    View Slide

  21. 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 Slide

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

    View Slide

  23. Borrowing

    View Slide

  24. Borrows
    are
    Pointers

    View Slide

  25. Borrows
    are
    References

    View Slide

  26. &

    View Slide

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

    View Slide

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

    View Slide

  29. 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 Slide

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

    View Slide

  31. Types?

    View Slide

  32. We’ve got
    types!

    View Slide

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

    View Slide

  34. Structs

    View Slide

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

    View Slide

  36. Enums

    View Slide

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

    View Slide


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

    View Slide

  39. Null?

    View Slide

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

    View Slide

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

    View Slide

  42. 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 Slide

  43. Try?

    View Slide

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

    View Slide

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

    View Slide

  46. Further
    Typing

    View Slide

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

    View Slide

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

    View Slide

  49. Fearless
    Concurrency

    View Slide

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

    View Slide

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

    View Slide

  52. Rayon

    View Slide

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

    View Slide

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

    View Slide

  55. Fireflowers
    &
    Unsafe

    View Slide

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

    View Slide

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

    View Slide

  58. unsafe

    View Slide

  59. crates.io
    Rust is More

    View Slide

  60. View Slide

  61. Any questions?

    View Slide

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

    View Slide