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

Rust Introduction @ Rust Meetup Taipei #1

Rust Introduction @ Rust Meetup Taipei #1

Kan-Ru Chen

April 21, 2016
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. Getting Started • The Rust Programming Language Book ◦ Rust

    程式語言 • Rust Primer • Rust Playground
  3. Rust Syntax struct RustTw { talk: &’static str } impl

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

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

    int = 42; return &answer; } fn find_answer() -> int { return *compute(); //Error }
  6. 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
  7. Ownership Of Memory // C++ int *a = new int;

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

    b = a; println!("{}", a); test.rs:4:21: 4:22 error: use of moved value: `a` test.rs:4 println!("{}", *a); ^
  9. 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).
  10. 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()); } }
  11. 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
  12. 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
  13. Minimum Core Language • Aimed for System Programming • Standard

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

    Generics • Pattern Matching • Named lifetime • Module system • ...
  15. Tools around Rust • Cargo ◦ Crates.io • Editor Support

    ◦ Vim, Emacs, Atom, Visual Studio Code • Rustdoc • Racer • Rustfmt
  16. 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. • Redox OS: a new posix like OS written in Rust.