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

Rust @ OSDC.tw 2014

Rust @ OSDC.tw 2014

Kan-Ru Chen

April 12, 2014
Tweet

More Decks by Kan-Ru Chen

Other Decks in Programming

Transcript

  1. Rust Rust is a programming language with a focus on

    type safety, memory safety, concurrency and performance.
  2. Rust Syntax struct OSDC_tw { talk: ~str } impl OSDC_tw

    { fn hello(&self) -> () { println!(“Hello {}”, self.talk); } } fn main() { let osdc = OSDC_tw{ talk: ~“Rust” }; osdc.hello(); }
  3. Unsafe C Program int *compute(void) { int answer = 42;

    return &answer; } int find_answer() { return *compute(); //Crash }
  4. Incorrect Rust Program fn compute() -> &int { let answer:

    int = 42; return &answer; } fn find_answer() -> int { return *compute(); //Error }
  5. test.rs:3:12: 3:19 error: `answer` does not live long enough test.rs:3

    return &answer; ^~~~~~~ test.rs:1:22: 4:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 1:21... test.rs:1 fn compute() -> &int { test.rs:2 let answer: int = 42; test.rs:3 return &answer; test.rs:4 } test.rs:1:22: 4:2 note: ...but borrowed value is only valid for the block at 1:21 test.rs:1 fn compute() -> &int { test.rs:2 let answer: int = 42; test.rs:3 return &answer; test.rs:4 } error: aborting due to previous error
  6. Ownership Of Memory // C++ int *a = new int;

    *a = 123; int *b = a; free(b); printf(“%d\n”, *a);
  7. Ownership Of Memory // Rust let a = ~123; let

    b = a; println!("{}", *a); test.rs:4:21: 4:22 error: use of moved value: `a` test.rs:4 println!("{}", *a); ^
  8. Two Pointer Types • Uniquely owned pointer let a =

    ~42; • Borrowed pointer let a = &42;
  9. Which Pointer Should I Use? • Always use data allocated

    on the stack as much as possible. • Use owned pointer when you can decide the object’s lifetime. • Use borrowed pointer when you know the object’s lifetime. • Use reference counted ...
  10. Stack Closure let square = |x: int| x*x; let mut

    max = 0; let f = |x: int| if x > max { max = x }; for x in [1, 2, 3].iter() { f(*x); } return f; // error
  11. Owned Closure let answer = ~42; spawn(proc() { println!("I'm a

    new task"); // Capture answer in remote task println!("{}", answer); }); println!("{}", answer); // error
  12. Communication let (tx, rx): (Sender<int>, Receiver<int>) = channel(); spawn(proc() {

    let result = some_expensive_computation(); // Send a copy to remote tx.send(result); }); some_other_expensive_computation(); let result = rx.recv();
  13. Selectable Runtime • 1:1 - using libnative ◦ Guaranteed interop

    with FFI bindings. ◦ Less I/O overhead in some cases. • M:N - using libgreen ◦ Fast task spawning. ◦ Fast task switching.
  14. C Compatibility Structs are laid out the same way in

    C in memory (so you can read from a Rust struct in C, and vice-versa).
  15. Foreign Libraries extern crate libc; use libc::c_uint; #[link(name = "gtk-3")]

    extern { fn gtk_get_major_version() -> c_uint; } fn main() { unsafe { // Call unsafe function println!("gtk major version {}", gtk_get_major_version()); } }
  16. Calling Rust From C // Rust #[no_mangle] pub extern fn

    hello() { println!("Hello World!"); } // C extern void hello(); int main(int argc, char* argv[]) { hello(); } gcc test.c -llib-b47d76b0-0.0 -L. -lpthread -lm -ldl
  17. Standard Library std::vec::Vec std::sync std::comm std::mem std::str std::rc::Rc • Crates

    ◦ sync ◦ getopts ◦ libc ◦ log ◦ rand ◦ term ◦ url ◦ uuid
  18. Minimum Core Language • Aimed for System Programming • Standard

    Library ◦ Thread ◦ Channel ◦ GC / Reference Counted Pointer
  19. Not in this talk • Algebra Types • Traits •

    Generics • Pattern Matching • Named lifetime • Module system • ...
  20. Rust “A type-safe Erlang, a scrubbed-up C++, a not- stupidly-weak

    Go, a fast Haskell, a fast ML, a history-sanitized Lisp; it's a real hodgepodge, and that's no criticism.” -- ncm on LWN
  21. Projects Using Rust • Rust: the compiler, 60,000+ lines at

    the time of writing, is written in Rust. • Servo: a prototype web parallel browser engine written in the Rust language. • Sprocketnes: NES emulator written in Rust as a technology demonstration. • Boot2Rust: EFI applications in Rust.