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

Rust: low-level programming without the segfaults

Rust: low-level programming without the segfaults

Presented at OSCON 2013.

dherman

July 25, 2013
Tweet

More Decks by dherman

Other Decks in Programming

Transcript

  1. • Safe unions and pointers • Explicit memory layout &

    stack allocation • Optional garbage collection • Mutability control & race-free concurrency Rust: perf + safety
  2. struct Tree { enum TreeTag tag; union { int leaf;

    Pair node; } data; }; enum TreeTag { Empty, Leaf, Node }; struct Pair { Tree *left, *right; };
  3. match tree { Empty => 0, Leaf(_) => 1, Node(~ref

    left, ~ref right) => f(left) + f(right) }
  4. void *realloc(void *, ...); void free(void *); template<class K, class

    V> class Hashtable { vector<Bucket<K,V>> buckets; ... }; Owned pointers
  5. memcpy(void *dest, const void *src, uintptr_t count); template<class K, class

    V> class Hashtable { ... V& find(K& key); }; Temporary pointers
  6. { let b = ~[]; let table = Hashtable {

    buckets: b }; ... } heap allocate
  7. { let b = ~[]; let table = Hashtable {

    buckets: b }; ... } heap allocate stack allocate
  8. { let b = ~[]; let table = Hashtable {

    buckets: b }; ... } heap allocate stack allocate move
  9. fn move_from() { let x: ~int = ~22; move_to(x); }

    fn move_to(p: ~int) { printfln!(“%d”, *p); }
  10. fn move_from() { let x: ~int = ~22; move_to(x); }

    fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate
  11. fn move_from() { let x: ~int = ~22; move_to(x); }

    fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate move
  12. fn move_from() { let x: ~int = ~22; move_to(x); }

    fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate free p move
  13. fn move_from() { let x: ~int = ~22; move_to(x); }

    fn move_to(p: ~int) { printfln!(“%d”, *p); } heap allocate free p move printfln!(“%d”, *x); // error
  14. fn f() { let x: ~int = ~22; borrow(x); printfln!(“%d”,

    *x); } fn borrow(x: &int) { printfln!(“borrowing: %d”, *x); }
  15. fn f() { let x: int = 22; borrow(&x); printfln!(“%d”,

    *x); } fn borrow(x: &int) { printfln!(“borrowing: %d”, *x); }
  16. let w = ~22; { let x = &w; let

    y = &*x; } ‘a ‘b ‘c ‘d Lifetimes
  17. fn find<‘a,K,V>(tbl: &’a Hashtable<K,V>, key: &K) -> Option<&’a V> {

    ... } pointer with lifetime ‘a to table pointer with lifetime ‘a to value
  18. let (port, chan) = stream(); do spawn { let p

    = ~Point { x: 1.0, y: 2.0 }; chan.send(p); } let p = port.recv(); printfln!(p.to_str());
  19. • Pluggable I/O • Optional scheduler • Data race-free shared

    data structures • Potential for race-free fork-join parallelism • Working on SIMD and GPU libs There’s more
  20. 1. Don’t use unsafe code. 2. If you absolutely must

    break Rule 1, • get out of unsafe code ASAP, and • always provide a safe interface at the boundaries. The rules
  21. Image credits Sean Martell http://blog.seanmartell.com Ben Clinch http://www.flickr.com/photos/benclinch/3048906022/ juicyrai http://www.flickr.com/photos/wink/180979062/

    Rama, Wikimedia Commons http://en.wikipedia.org/wiki/File:Sir_Tony_Hoare_IMG_5125.jpg Jared Zimmerman http://www.flickr.com/photos/spoinknet/7932382540/ CCAC North Library http://www.flickr.com/photos/ccacnorthlib/3553821699/ Mike Hayes http://www.flickr.com/photos/rotties/449650275/