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

F51da3602a201d3dca41fa924e29b29d?s=128

Will Speak

April 25, 2018
Tweet

Transcript

  1. Will Speak - Developer A Rusty Guide to Types and

    Safety or Let’s Rewrite it in Rust!
  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.
  3. Will Speak - Developer A Rusty Guide to Types and

    Safety or Let’s Rewrite it in Rust!
  4. @willspeak iwillspeak Will Speak

  5. We’re Hiring!

  6. Rust!

  7. Rust What is rust? Types How types can help you

    Concurrency Fearless parallel Advanced Unsafe code and more
  8. What is Rust?

  9. Systems Programming Language

  10. Safe, Concurent, Fast

  11. Safe, Concurent, Fast Pick Three

  12. Safety

  13. Hello Rust!

  14. 
 
 // Hello world, in Rust! fn main() {

    println!("Hello, world!"); } Hello Rust
  15. Ownership

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

  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
  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
  19. Mutation

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

    book.next_page(); Mutation
  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
  22. let mut book = Book::new("Matter"); // OK now, book is

    mutable book.next_page(); Mutation
  23. Borrowing

  24. Borrows are Pointers

  25. Borrows are References

  26. &

  27. let my_book = Book::new("Inversions"); let my_borrow = &my_book; println!("Original book:

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

    &my_book; } println!("Borrowed book: {:?}", my_borrow); Borrowing
  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
  30. let mut my_book = Book::new(“Inversions"); let my_borrow: &mut Book =

    &my_book; my_book.next_page(); Borrowing
  31. Types?

  32. We’ve got types!

  33. Integers i32 i64 u8 u32 … Strings String &str Booleans

    bool Arrays & Slices [u32; 6] &[i64] Tuples (i8, String, bool)
  34. Structs

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

  36. Enums

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

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

    Lend(title) => println!("Lending {}", title), _ => () } Enums
  39. Null?

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

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

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

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

  45. pub fn hello_to_file(path: &Path) -> Result<(), Error> { let mut

    file = File::create(&path)?; write!(file, "hello world")?; Ok(()) } Try?
  46. Further Typing

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

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

  49. Fearless Concurrency

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

  51. let lock = mutex.lock(); match lock { Ok(mut guard) =>

    { *guard += 1; println!("Value: {0}", guard); } Err(_) => println!("Mutex poisoned") } Mutex<T>
  52. Rayon

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

    i) .sum() } Rayon
  54. use rayon::prelude::*; fn sum_of_squares(input: &[i32]) -> i32 { input.par_iter() //

    <-- just change that! .map(|&i| i * i) .sum() } Rayon
  55. Fireflowers & Unsafe

  56. struct ListNode<T, 'a> { data: T, next: &'a ListNode<T, 'a>,

    prev: &'a ListNode<T, 'a>, } The Linked List
  57. struct ListNode<T> { data: T, next: *mut ListNode<T>, prev: *mut

    ListNode<T>, } Breaking Out
  58. unsafe

  59. crates.io Rust is More

  60. None
  61. Any questions?

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