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

Rust Programming Language

Eric
March 27, 2015

Rust Programming Language

A presentation given for my CSCI 410, Elements of Computing Systems, course.

Eric

March 27, 2015
Tweet

Other Decks in Programming

Transcript

  1. IT IS... ▸ brand new ▸ backed by Mozilla ▸

    not yet at version 1.0 ▸ a systems language ▸ immutable by default ▸ memory-safe
  2. IT DOES... ▸ prevent data races ▸ use trait-based generics

    ▸ interoperate with C ▸ enforce lifetimes to prevent use-after-free
  3. OWNERSHIP ▸ Rigorously keeps track of which variables own resources

    ▸ Pass-by-value, but moves ownership ▸ Lifetimes are used to verify when memory enters and exits scope
  4. LIFETIMES ▸ Annotations that indicate how long memory is valid

    for ▸ Lifetime elision allows you to omit them in most cases ▸ Compiler will infer lifetimes automatically
  5. OWNERSHIP fn main() { let my_alloc_mem = Box::new(5i32); consumer(my_alloc_mem); println!("{}",

    my_alloc_mem); // Error! } fn consumer(consumed_val: Box<i32>) { // Do nothing, just consume value }
  6. OWNERSHIP (CONT.) main.rs:18:11: 18:23 note: `my_alloc_mem` moved here because it

    has type `Box<i32>`, which is non-copyable — The Rust Compiler
  7. BORROWING fn main() { let my_alloc_mem = Box::new(5i32); borrower(&my_alloc_mem); println!(“{}”,

    my_alloc_mem); // works this time! } fn borrower(borrowed_val: &Box<i32>) { // Do nothing with borrowed value }
  8. LIFETIMES Elided: fn substr(s: &str, until: u32) -> &str Explicit:

    fn substr<‘a>(s: &’a str, until: u32) -> &’a str
  9. NON-CONSTRAINED GENERICS: fn generic_function<T>() { ... } CONSTRAINED GENERICS: fn

    cmp<T: PartialOrd>(&T comp_object) { ... } fn cmp<T>(&T comp_object) where T: PartialOrd { ... }
  10. CONCURRENCY use std::thread::scoped; use std::sync::Arc; fn main() { let mut

    guards = vec![]; let thread_numbers: Vec<i32> = (0..10).collect(); for i in 0..10 { let thread_num = Arc::new(thread_numbers[i]); guards.push(scoped(move || { let index = thread_num.clone(); println!(“Hello from thread #{}”, index); })); } for guard in guards.into_iter() { guard.join(); } }