Slide 1

Slide 1 text

ACCU 2018 Hacker’s guide to Rust Programming Vigneshwer Dhinakaran | Mozilla TechSpeaker | @dvigneshwer

Slide 2

Slide 2 text

Zen of Rust Memory Safety without Garbage Collection No runtime needed Rich Typesystem High level iterators Freedom from data race Welcoming Community

Slide 3

Slide 3 text

Facts & Figures

Slide 4

Slide 4 text

Speeding up crypto on Wire desktop apps https://medium.com/@wireapp/speeding-up-crypto-on-wire-desktop-apps-3ff37fc98c3f Up to 141× faster

Slide 5

Slide 5 text

Tokio MiniHTTP The library is a proof-of-concept implementation of a simple HTTP/1.1 server using Tokio. http://aturon.github.io/blog/2016/08/11/futures/

Slide 6

Slide 6 text

2016 -18 For the third year in a row, Rust is stackoverflow's most loved language https://insights.stackoverflow.com/survey/2018/

Slide 7

Slide 7 text

Friends of Rust Organizations running Rust in production. (https://www.rust-lang.org/en-US/friends.html)

Slide 8

Slide 8 text

History: How did it all begin?

Slide 9

Slide 9 text

Browsers Compete for performance & need Safety at its core

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Gradual adoption works.

Slide 13

Slide 13 text

13

Slide 14

Slide 14 text

Rust is easy to learn!

Slide 15

Slide 15 text

Variables

Slide 16

Slide 16 text

Functions

Slide 17

Slide 17 text

Flow control

Slide 18

Slide 18 text

Flow control

Slide 19

Slide 19 text

Looping

Slide 20

Slide 20 text

Types

Slide 21

Slide 21 text

Package Manager

Slide 22

Slide 22 text

Ownership The act, state, or right of possessing something

Slide 23

Slide 23 text

Garbage Collection Heap Memory new Circle(5) new Circle(5) new Circle(5)

Slide 24

Slide 24 text

Referencing Now both bookingService and repairService hold a reference to vehicle

Slide 25

Slide 25 text

Same implementation in Rust Doesn't compile!

Slide 26

Slide 26 text

Ownership ● Rust moves ownership by default ● The owner has the right to destroy the thing it owns ● The memory is freed as soon as the owned variable goes out of it's scope ● Hence vehicle may already be destroyed at the point when it's passed to repair_service ● Rust catches these errors at compile time

Slide 27

Slide 27 text

Borrowing The act, state, or right of possessing something

Slide 28

Slide 28 text

Making it work in Rust Lending things is the key!

Slide 29

Slide 29 text

Borrowing ● A reference is passed without transferring ownership ● One can borrow immutable (&) or mutable (&mut) but not both at the same time ● Shared ownership can be achieved through special pointers with runtime checks

Slide 30

Slide 30 text

Mutability Rules ● All variables are immutable by default ● Only one mutable reference at a time ( But as many immutable &’s as you want ) ● Mutable references block all other access (The &mut must go out of scope before using other &’s )

Slide 31

Slide 31 text

Safe memory management without GC

Slide 32

Slide 32 text

High-level iterators Execution time : ~ 250 ms

Slide 33

Slide 33 text

High-level iterators

Slide 34

Slide 34 text

Rayon ~ 100 ms

Slide 35

Slide 35 text

Struct ● Structs are like lightweight classes ● They can have fields ● They can be instantiated

Slide 36

Slide 36 text

Struct But not partially !

Slide 37

Slide 37 text

Struct Rust has option enum for such cases

Slide 38

Slide 38 text

Struct Every Option is either Some and contains a value, or None, and does not. Which makes it explicit (No NullPointerExceptions!)

Slide 39

Slide 39 text

Struct Every Option is either Some and contains a value, or None, and does not. Which makes it explicit (No NullPointerExceptions!)

Slide 40

Slide 40 text

Struct can have methods

Slide 41

Slide 41 text

Struct can have methods

Slide 42

Slide 42 text

Traits ● Traits are Rust’s sole notion of interface ● Traits can be statically dispatched ● Traits can be dynamically dispatched ● Traits solve a variety of additional problems beyond simple abstraction

Slide 43

Slide 43 text

Traits

Slide 44

Slide 44 text

Traits

Slide 45

Slide 45 text

Traits Traits can have default implementations

Slide 46

Slide 46 text

Traits Traits can be written for foreign types implementations!

Slide 47

Slide 47 text

Traits Traits can be written for foreign types implementations!

Slide 48

Slide 48 text

Generics What if the app need SmartCarStore as well ?

Slide 49

Slide 49 text

Generics Repetitive code

Slide 50

Slide 50 text

Generics Type safety becomes questionable

Slide 51

Slide 51 text

Generics Better :)

Slide 52

Slide 52 text

Generics Can use trait bounds

Slide 53

Slide 53 text

Generics Can pass any reference to anything that implements the Talk trait It has to be looked up in a vtable at run time ~ DYNAMIC DISPATCH

Slide 54

Slide 54 text

Generics talk is now generic with a Talk trait bound

Slide 55

Slide 55 text

Generics The compiler generates specialized code ~ Static Dispatch Zero -Cost Abstraction !!

Slide 56

Slide 56 text

Enum Error can either be Critical or NonCritical at any one time

Slide 57

Slide 57 text

Error Handling Variants may contain data of any other (mixed) types

Slide 58

Slide 58 text

Error Handling Types can be generic In fact the std library uses a lot of generic enums

Slide 59

Slide 59 text

Error Handling Enums can implement methods

Slide 60

Slide 60 text

Error Handling Enums can implement traits

Slide 61

Slide 61 text

Macros Macros allow us to abstract at a syntactic level

Slide 62

Slide 62 text

Rust Web framework Nickel

Slide 63

Slide 63 text

Web application framework Thin layer on top of pure http (Inspired by express.js)

Slide 64

Slide 64 text

Web application framework Custom middleware

Slide 65

Slide 65 text

Other Salient features ● Custom Middleware ● JSON Support ● Mounting ● Error Handling ● Encrypted Cookies ● Session Support

Slide 66

Slide 66 text

Using Rust with Node.js

Slide 67

Slide 67 text

Application to generate Fibonacci numbers /Cargo.toml ~ package.json https://github.com/RisingStack/node-with-rust /src/embed.rs

Slide 68

Slide 68 text

Node.js Integration You are calling Rust from Node.js now!

Slide 69

Slide 69 text

Deep dive into Neon project

Slide 70

Slide 70 text

Getting Started

Slide 71

Slide 71 text

Hello world native/Cargo.toml /native/src/lib.rs https://guides.neon-bindings.com/hello-world/

Slide 72

Slide 72 text

Hello world lib/index.js Build & Run https://guides.neon-bindings.com/hello-world/

Slide 73

Slide 73 text

call.scope eventHandler initObject init_obj init_prop set_prop Object::set setter JavaScript Rust V8 JavaScript GC’ed Heap Memory

Slide 74

Slide 74 text

Rust Tools

Slide 75

Slide 75 text

Tools ● rustup: the Rust toolchain installer ● rustfmt : the Rust code formatting tool ● rust-clippy : Rust code linter ● Cargo : package manager ● ...

Slide 76

Slide 76 text

76 Community Photo credit: David McSpadden https://www.flickr.com/photos/familyclan/15535822737/

Slide 77

Slide 77 text

77 RFC Process

Slide 78

Slide 78 text

78

Slide 79

Slide 79 text

Summary

Slide 80

Slide 80 text

When should you use Rust? ● Rewrite modules of your code that are computationally heavy (Complex math optimizations etc) ● For accessing the hardware(ex :- IOT applications, device drivers, GPU’s etc) ● Implementing advance concurrency paradigms ● Rust FFI calls are non-trivial, so be wise!

Slide 81

Slide 81 text

What does Rust have to offer? ● Modern C/C++ replacement ● Memory Safety without Garbage Collection ● No runtime needed ● Rich Typesystem ● Strong Functional Programming influence

Slide 82

Slide 82 text

Thanks! @dvigneshwer [email protected] https://tinyurl.com/accurust