Slide 1

Slide 1 text

Introduction to Rust

Slide 2

Slide 2 text

Rust - Created by Graydon Hoare - Funded by Mozilla - Primary goal: A safer systems language - First version: 2010 - Current version 1.5 - Main project: Servo browser engine

Slide 3

Slide 3 text

Systems Programming Memory management Error handling Static Typing Thread safety Compiling …

Slide 4

Slide 4 text

Systems Programming is hard!

Slide 5

Slide 5 text

• Provide the right abstractions • Provide awesome tools • Allow making mistakes • Build a helpful community Systems Programming can be fun!

Slide 6

Slide 6 text

RUST: High level on bare metal

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Where Rust makes sense • Operating Systems • Encryption • Databases • Graphics programming • … (everywhere you need speed and safety)

Slide 9

Slide 9 text

Where Rust makes sense glium • High-level wrapper around OpenGL • Avoids all OpenGL errors diesel • A safe, extensible ORM and Query Builder • Operating System written in pure Rust,
 designed to be modular and secure Redox crossbeam • A collection of lock-less data structures turbine • A high-performance, non-locking, inter-task communication library written in Rust. • Go channels on steroids

Slide 10

Slide 10 text

What I like about… • PHP: Package manager, Community • Python: Syntax, Libraries • Golang: Tooling, Documentation, Concurrency • C: Speed, no overhead

Slide 11

Slide 11 text

What I like about… • PHP: Package manager, Community • Python: Syntax, Libraries • Golang: Tooling, Documentation, Concurrency • C: Speed, no overhead RUST

Slide 12

Slide 12 text

What sucks about… • PHP: Syntax (a bit), legacy functions • Python: Package manager, 2 vs 3 • Golang: Error handling, package manager, no generics, Syntax (a bit) • C: Missing package manager, safety

Slide 13

Slide 13 text

Complexity vs Speed • PHP • Python • Golang • Rust • C Faster „Easier“

Slide 14

Slide 14 text

Popular systems languages Type safety Type expression Type checking Garbage Collector C unsafe explicit static No C++ unsafe explicit static No Go safe implicit/explicit static Yes Rust safe implicit/explicit static/dynamic optional

Slide 15

Slide 15 text

#include #include #include int main() { std::vector v; v.push_back("foo"); auto const &x = v[0]; v.push_back("bar"); // Undefined behavior! std::cout << x << '\n'; }

Slide 16

Slide 16 text

What went wrong? Your vector is just an address in memory

Slide 17

Slide 17 text

When it grows, it might be moved! What went wrong?

Slide 18

Slide 18 text

Your old pointer still exists. What went wrong?

Slide 19

Slide 19 text

…but it points to „something“ What went wrong?

Slide 20

Slide 20 text

There’s multiple mutable references to the same data Observation

Slide 21

Slide 21 text

There’s multiple mutable references to the same data Ownership Avoid There should only be one owner at the same time In Rust this is enforced by the compiler!

Slide 22

Slide 22 text

Is this really an issue? Search results on StackOverflow.com: segfault 10.899 results segmentation fault 21.238 results memory leak 55.064 results

Slide 23

Slide 23 text

Ownership + Borrowing + Lifetimes = data safety Three important concepts in Rust

Slide 24

Slide 24 text

Ownership in real life Me You Here’s a present! Book owner

Slide 25

Slide 25 text

Ownership in real life Me You Thanks Dude! Book owner

Slide 26

Slide 26 text

Ownership in real life Me You Book owner

Slide 27

Slide 27 text

Borrowing in real life Me You Nice book! Can I lend it? Book owner

Slide 28

Slide 28 text

Borrowing in real life Me You Thanks Dude! Sure! Book owner

Slide 29

Slide 29 text

Borrowing in real life Me You Book owner

Slide 30

Slide 30 text

Borrowing in real life Me You Thanks for lending Book owner

Slide 31

Slide 31 text

fn f(x: Type) {...} fn f(x: &Type) {...} fn f(x: &mut Type) {...} Shared borrow Mutable borrow Ownership • one owner • mutable • readable • Share as you like • immutable • readable • one at a time • mutable • readable

Slide 32

Slide 32 text

Show us some code already!

Slide 33

Slide 33 text

What sucks about Rust • Fighting with the Borrow checker • Syntax (a bit) • Missing packages (you can help!) • probably more…

Slide 34

Slide 34 text

Tool list Unit testing: builtin Package manager Syntax highlighting Formatting Code completion Debugging Code linter Benchmarking Profiling Code coverage

Slide 35

Slide 35 text

https://doc.rust-lang.org/book/testing.html Unit testing

Slide 36

Slide 36 text

Unit testing: builtin Package manager: cargo Syntax highlighting Formatting Code completion Debugging Code linter Benchmarking Profiling Code coverage Tool list

Slide 37

Slide 37 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting Code completion Debugging Code linter Benchmarking Profiling Code coverage Tool list

Slide 38

Slide 38 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion Debugging Code linter Benchmarking Profiling Code coverage Tool list

Slide 39

Slide 39 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging Code linter Benchmarking Profiling Code coverage Tool list

Slide 40

Slide 40 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter Benchmarking Profiling Code coverage Tool list

Slide 41

Slide 41 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking Profiling Code coverage Tool list

Slide 42

Slide 42 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling Code coverage Tool list

Slide 43

Slide 43 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… Code coverage Tool list

Slide 44

Slide 44 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… torch? Code coverage Tool list

Slide 45

Slide 45 text

https://llogiq.github.io/2015/07/15/profiling.html Calls Time

Slide 46

Slide 46 text

Unit testing: builtin Package manager: cargo Syntax highlighting: atom-language-rust Formatting: rustfmt Code completion: racer Debugging: rust gdb? Code linter: clippy Benchmarking: builtin Profiling: meh… torch? Code coverage: kcov? Tool list

Slide 47

Slide 47 text

kcov …is a code coverage tester for compiled programs, Python scripts and shell scripts language: rust matrix: fast_finish: true include: - rust: nightly env: FEATURES="--features nightly" sudo: false - rust: nightly env: FEATURES="--features nightly" BENCH=true sudo: false - rust: beta sudo: true after_success: | sudo apt-get install libcurl4-openssl-dev libelf-dev libdw-dev && wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && tar xzf master.tar.gz && mkdir kcov-master/build && cd kcov-master/build && cmake .. && make && sudo make install && cd ../.. && kcov --coveralls-id=$TRAVIS_JOB_ID --exclude-pattern=/.cargo target/kcov target/debug/hyper-* Put this into your .travis.yml:

Slide 48

Slide 48 text

Summary Lots of work to do But impressive start

Slide 49

Slide 49 text

We need to focus on those… Unit testing Package manager Syntax highlighting Formatting Code completion Debugging Code linter Benchmarking Profiling Code coverage

Slide 50

Slide 50 text

Bonus Rustlings (https://github.com/carols10cents/rustlings) Euler Rust (https://github.com/gifnksm/ProjectEulerRust) Rust libraries/packages (https://crates.io/)
 
 Rust patterns
 (https://github.com/nrc/patterns)

Slide 51

Slide 51 text

Even more at https://gist.github.com/nrc/a3bbf6dd1b14ce57f18c