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

Rust in 10 Minutes

John Downey
January 29, 2015

Rust in 10 Minutes

Given as a language lightning talk at Braintree

John Downey

January 29, 2015
Tweet

More Decks by John Downey

Other Decks in Technology

Transcript

  1. Rust 4 Systems language (would use it instead of C/C++)

    4 From Mozilla Research 4 Goals 4 Safe 4 Concurrent 4 Fast 2
  2. 4

  3. 5

  4. Immutable by Default fn main() { let a = 1;

    let mut b = 1; b += 1; a += 1; } 7
  5. Immutable by Default $ rustc mutability.rs mutability.rs:6:5: 6:11 error: re-assignment

    of immutable variable `a` mutability.rs:6 a += 1; ^~~~~~ mutability.rs:2:9: 2:10 note: prior assignment occurs here mutability.rs:2 let a = 1; ^ error: aborting due to previous error 8
  6. Ownership and Moving fn my_print(s: String) { println!("s: {}", s);

    } fn main() { let a = "test".to_string(); my_print(a); println!("a: {}", a); } 9
  7. Ownership and Moving $ rustc ownership.rs ownership.rs:8:23: 8:24 error: use

    of moved value: `a` ownership.rs:8 println!("a: {}", a); ^ ownership.rs:7:14: 7:15 note: `a` moved here because it has type `collections::string::String`, which is non-copyable ownership.rs:7 my_print(a); ^ error: aborting due to previous error 10
  8. Ownership and Moving fn my_print(s: String) { // ^ taking

    ownership here println!("s: {}", s); // so this is implied // free(s); } fn main() { let a = "test".to_string(); my_print(a); println!("a: {}", a); } 11
  9. Borrowing fn my_print(s: &String) { println!("s: {}", s); } fn

    main() { let a = "test".to_string(); my_print(&a); println!("a: {}", a); } 12
  10. Stack and Heap 4 Values are stack allocated by default

    4 Things can be "boxed" or heap allocated fn my_print(b: Box<i32>) { println!("b: {}", b); // this is implied // free(b); } fn main() { let a = Box::new(5); // let a = box 5; my_print(a); // this would be an moved/ownership error // println!("a: {}", a); } 13
  11. Automatic Memory Management 4 Ownership and borrow checking 4 Know

    the lifetime of objects 4 Not possible to use a pointer after it is freed 4 Also not possible to use uninitialized memory 4 Heap memory is freed automatically, no GC! 4 All enforced by the compiler 14
  12. Unsafe Code 4 Allows you to break all the rules

    4 Able to call into C code 4 Should minimize and wrap in safe code 15
  13. Type System 4 Generics 4 Type inference 4 Traits instead

    of classes (similar to typeclasses) 4 Algebraic Data Types 4 No notion of null 4 Replaced with Option<T> type 16
  14. Primitives 4 Native threads 4 Thread/task pool 4 Atomics (bool,

    int, etc) 4 Synchronization primitives 4 Mutex 4 Future 18
  15. Channels use std::thread::Thread; use std::sync::mpsc::channel; fn main() { let (tx,

    rx) = channel(); Thread::spawn(move|| { tx.send(10i).unwrap(); }); println!("{}", rx.recv().unwrap()); } 19
  16. Data Races 4 Ownership system prevents many types of dataraces

    4 Only one thread can hold a mutable reference 4 Unless you mark your type as thread safe 4 Enforced by compiler 20
  17. Single Instruction Multiple Data use std::simd::f32x4; fn main() { //

    create simd vectors let x = f32x4(1.0, 2.0, 3.0, 4.0); let y = f32x4(4.0, 3.0, 2.0, 1.0); // simd product let z = x * y; // like any struct, the simd vector can be destructured let f32x4(a, b, c, d) = z; println!("z: {}", (a, b, c, d)); } 22
  18. Inline Assembly (feature gated) #![feature(asm)] fn add(a: i32, b: i32)

    -> i32 { let sum: i32; unsafe { asm!("add $2, $1; mov $1, $0" : "=r"(sum) : "r"(a), "r"(b)); } sum } 24
  19. Minimal Runtime 4 Possible to call into Rust from C

    4 Or anything that can have C bindings 4 Like Ruby! gem: rust_example 4 Tries to be low level but safe 25
  20. Other Fun Stuff 4 Hygienic macros 4 Pattern matching 4

    Closures and higher order functions 4 Statically linked binaries 26
  21. Cargo - https://crates.io 4 A crate is a unit of

    compilation (binary/library) 4 Mozilla hired developers of Bundler 4 Manages dependencies and versions 4 Builds code 4 Runs tests 27
  22. Downsides 4 Language changes a lot 4 There is an

    upcoming 1.0 release 4 Ownership system is complicated 4 Compiler is smart, but not forgiving 4 Built-in test framework can be annoying 4 Easy to get confused (ex: two types of strings) 28
  23. 29

  24. Getting started 4 Book - http://doc.rust-lang.org/book/ 4 Rust by Example

    - http://rustbyexample.com/ 4 Reddit - http://www.reddit.com/r/rust/ 4 This Week in Rust - http://this-week-in-rust.org/ 4 IRC - #rust on irc.mozilla.org 30