Slide 1

Slide 1 text

Introduction to Rust [email protected] github.com/luikore

Slide 2

Slide 2 text

Design • No VM, no GC, compile time checks • Trait based, functional • LLVM as compiler backend • Comes with a Package and Build System: cargo

Slide 3

Slide 3 text

Design • Similar to Swift in many aspects • https://lobste.rs/s/mc9rgv/ rust_creator_graydon_hoare_s_thoughts_on_swift

Slide 4

Slide 4 text

History • Graydon Hoare (just left Mozilla) • Failure of ECMAScript4 • Appear from 2010 • Servo: Mozilla’s new browser engine

Slide 5

Slide 5 text

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)

Slide 6

Slide 6 text

Hello World • brew update; brew install rust • rustc hello.rs; ./hello

Slide 7

Slide 7 text

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 //

Slide 8

Slide 8 text

Notes on Struct

Slide 9

Slide 9 text

Notes on {:fmt}

Slide 10

Slide 10 text

Cargo crates • cargo new foo --bin • cargo build • cargo run

Slide 11

Slide 11 text

Trait Based System • Separate Data and Behavior • trait can act as interface or partial implementation • trait can inherit and form hierarchy

Slide 12

Slide 12 text

Trait Example

Slide 13

Slide 13 text

Generics • Reusing code for static typed language

Slide 14

Slide 14 text

Generics

Slide 15

Slide 15 text

We can Gives Compiler Much Info via Generics

Slide 16

Slide 16 text

Type Inference (why need it) • Verbosity of static typing: need to write down the type twice or more

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Type Inference (deduction) • C++ and Golang are only type deductions

Slide 19

Slide 19 text

Type Inference (generic inference) • Rust: generic enhanced inference

Slide 20

Slide 20 text

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)

Slide 21

Slide 21 text

Pointer Kinds • Managed — reference counted • Owned — not reference counted • Borrowed — borrow from some one else’s owned pointer

Slide 22

Slide 22 text

Null Pointer • params[:photos].each {…} # NoMethodError!

Slide 23

Slide 23 text

Null Pointer (solution) • Forbid use of possibly uninitialized variable • Option with Pattern Match

Slide 24

Slide 24 text

Pattern Match

Slide 25

Slide 25 text

Borrow Pointer • 0 to N references (&T) to a resource • exactly one mutable reference (&mut T)

Slide 26

Slide 26 text

Borrow Pointer Caveats

Slide 27

Slide 27 text

Dangling Pointer • rust forbids dangling pointers like this: Point* foo() { Point point = {.x = 1, .y = 2}; return &point; // bang! }

Slide 28

Slide 28 text

Dangling Pointer (solution) • Lifetime rules

Slide 29

Slide 29 text

http server • hyper (web frameworks: iron and nickel) • mio • redis-rs

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Rust by Example • http://rustbyexample.com/