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

Intro to Rust Programming Language

Zete
June 09, 2015

Intro to Rust Programming Language

Shared on Shanghai Ruby Tuesday

Zete

June 09, 2015
Tweet

More Decks by Zete

Other Decks in Programming

Transcript

  1. Design • No VM, no GC, compile time checks •

    Trait based, functional • LLVM as compiler backend • Comes with a Package and Build System: cargo
  2. History • Graydon Hoare (just left Mozilla) • Failure of

    ECMAScript4 • Appear from 2010 • Servo: Mozilla’s new browser engine
  3. Inspiration • D - better C++ with engineering features •

    Haskell - pure functional • Ruby - beautiful block syntax • Scala - adding functional to a legacy OO language • Cyclone - a type safe dialect of C (like Nim)
  4. Notes on Basic Syntax • fn is how you declare

    a function • Statements separate by ; • println! is a macro, first arg must be literal • Entrance is main • Comment with //
  5. Trait Based System • Separate Data and Behavior • trait

    can act as interface or partial implementation • trait can inherit and form hierarchy
  6. Type Inference (why need it) • Verbosity of static typing:

    need to write down the type twice or more
  7. Type Inference Variants • Hindley-Damas-Milner Inference (Haskell is a variant

    of H-M inference) • Poor man’s type system (only local inference) • Type deduction
  8. Pointers (or reference) • Pointer is memory address with type

    info • By default: pass by value, not by reference • In Rust, usually you don’t need pointers (passing small structs may be more efficient)
  9. Pointer Kinds • Managed — reference counted • Owned —

    not reference counted • Borrowed — borrow from some one else’s owned pointer
  10. Borrow Pointer • 0 to N references (&T) to a

    resource • exactly one mutable reference (&mut T)
  11. Dangling Pointer • rust forbids dangling pointers like this: Point*

    foo() { Point point = {.x = 1, .y = 2}; return &point; // bang! }
  12. Caveats • Syntax a bit longer: ->, let mut, …

    • Complex concept of pointer type and ownership • LLVM package is large, compiling large project is slow • No actor or CSP-style concurrency? • Too late out to compete for the market