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

Intro to Rust Programming Language

00e4a880b1262a125b5e342e4b536765?s=47 Zete
June 09, 2015

Intro to Rust Programming Language

Shared on Shanghai Ruby Tuesday

00e4a880b1262a125b5e342e4b536765?s=128

Zete

June 09, 2015
Tweet

More Decks by Zete

Other Decks in Programming

Transcript

  1. Introduction to Rust zete@odigo.jp github.com/luikore

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

    Trait based, functional • LLVM as compiler backend • Comes with a Package and Build System: cargo
  3. Design • Similar to Swift in many aspects • https://lobste.rs/s/mc9rgv/

    rust_creator_graydon_hoare_s_thoughts_on_swift
  4. History • Graydon Hoare (just left Mozilla) • Failure of

    ECMAScript4 • Appear from 2010 • Servo: Mozilla’s new browser engine
  5. 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)
  6. Hello World • brew update; brew install rust • rustc

    hello.rs; ./hello
  7. 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 //
  8. Notes on Struct

  9. Notes on {:fmt}

  10. Cargo crates • cargo new foo --bin • cargo build

    • cargo run
  11. Trait Based System • Separate Data and Behavior • trait

    can act as interface or partial implementation • trait can inherit and form hierarchy
  12. Trait Example

  13. Generics • Reusing code for static typed language

  14. Generics

  15. We can Gives Compiler Much Info via Generics

  16. Type Inference (why need it) • Verbosity of static typing:

    need to write down the type twice or more
  17. 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
  18. Type Inference (deduction) • C++ and Golang are only type

    deductions
  19. Type Inference (generic inference) • Rust: generic enhanced inference

  20. 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)
  21. Pointer Kinds • Managed — reference counted • Owned —

    not reference counted • Borrowed — borrow from some one else’s owned pointer
  22. Null Pointer • params[:photos].each {…} # NoMethodError!

  23. Null Pointer (solution) • Forbid use of possibly uninitialized variable

    • Option with Pattern Match
  24. Pattern Match

  25. Borrow Pointer • 0 to N references (&T) to a

    resource • exactly one mutable reference (&mut T)
  26. Borrow Pointer Caveats

  27. Dangling Pointer • rust forbids dangling pointers like this: Point*

    foo() { Point point = {.x = 1, .y = 2}; return &point; // bang! }
  28. Dangling Pointer (solution) • Lifetime rules

  29. http server • hyper (web frameworks: iron and nickel) •

    mio • redis-rs
  30. 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
  31. Rust by Example • http://rustbyexample.com/