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!
  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. Rust What is rust? Types How types can help you

    Concurrency Fearless parallel Advanced Unsafe code and more
  5. 
 
 // Hello world, in Rust! fn main() {

    println!("Hello, world!"); } Hello Rust
  6. 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
  7. 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
  8. 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
  9. let mut book = Book::new("Matter"); // OK now, book is

    mutable book.next_page(); Mutation
  10. &

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

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

    &my_book; } println!("Borrowed book: {:?}", my_borrow); Borrowing
  13. 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
  14. Integers i32 i64 u8 u32 … Strings String &str Booleans

    bool Arrays & Slices [u32; 6] &[i64] Tuples (i8, String, bool)
  15. 
 match operation { Return(book) => println!( "Returning {}", book.title),

    Lend(title) => println!("Lending {}", title), _ => () } Enums
  16. fn print_surname(person: Person) { if let Some(surname) = person.surname {

    println!("{}'s surname is {}", person.forename, surname); } else { println!("{} has no surname"); } } Null?
  17. pub fn hello_to_file(path: &Path) -> Result<(), Error> { let mut

    file = File::create(&path)?; write!(file, "hello world")?; Ok(()) } Try?
  18. let lock = mutex.lock(); match lock { Ok(mut guard) =>

    { *guard += 1; println!("Value: {0}", guard); } Err(_) => println!("Mutex poisoned") } Mutex<T>
  19. use rayon::prelude::*; fn sum_of_squares(input: &[i32]) -> i32 { input.par_iter() //

    <-- just change that! .map(|&i| i * i) .sum() } Rayon
  20. struct ListNode<T, 'a> { data: T, next: &'a ListNode<T, 'a>,

    prev: &'a ListNode<T, 'a>, } The Linked List