Share
Embed
Rust @ OSDC.tw 2014
Kan-Ru Chen
April 12, 2014
Transcript
32 slides
-
about:me Kan-Ru Chen • Debian Developer • Mozilla • C/C++,
Python, Lisp, Rust [email protected] CC-BY-SA -
Rust Rust is a programming language with a focus on
type safety, memory safety, concurrency and performance. -
Outline • Syntax • Memory Safety • Concurrency • Foreign
Function Interface -
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(); } -
Unsafe C Program int *compute(void) { int answer = 42;
return &answer; } int find_answer() { return *compute(); //Crash } -
Incorrect Rust Program fn compute() -> &int { let answer:
int = 42; return &answer; } fn find_answer() -> int { return *compute(); //Error } -
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 -
Ownership Of Memory // C++ int *a = new int;
*a = 123; int *b = a; free(b); printf(“%d\n”, *a); -
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); ^ -
Two Pointer Types • Uniquely owned pointer let a =
~42; • Borrowed pointer let a = &42; -
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 ... -
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 -
Owned Closure let answer = ~42; spawn(proc() { println!("I'm a
new task"); // Capture answer in remote task println!("{}", answer); }); println!("{}", answer); // error -
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(); -
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. -
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). -
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()); } } -
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 -
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 -
Minimum Core Language • Aimed for System Programming • Standard
Library ◦ Thread ◦ Channel ◦ GC / Reference Counted Pointer -
Not in this talk • Algebra Types • Traits •
Generics • Pattern Matching • Named lifetime • Module system • ... -
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 -
Not Done Yet • 0.10 current stable • 0.11-pre •
Syntax is mostly stablized -
Community • GitHub: http://github.com/mozilla/rust http://github.com/rust-lang • Reddit: http://reddit.com/r/rust • Mailing
List: https://mail.mozilla. org/listinfo/rust-dev • IRC: #rust @ irc.mozilla.org -
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.