why rust? - matthias endler - codemotion amsterdam 2016
TRANSCRIPT
Why Rust?
- Düsseldorf, Germany
- Backend Engineer at trivago
- Website performance team
- Worked a lot with Python and PHP
- Likes hot chocolate
@matthiasendler
Matthias Endlermre
C
me
C++
System Programming
Memory management
Error handling
Static Typing
Compiling
…
Rust
- Created by Graydon Hoare
- Funded by Mozilla
- First version: 2010
- Current stable version 1.8
A safe systems language
System Programmingis challenging!
• Provide the right abstractions
• Provide awesome tools
• Allow making mistakes
• Build a helpful community
System Programming
can be fun!
Complexity vs Speed• PHP
• Python
• Golang
• Rust
• C
Faster „Easier“
RUST: High level on bare metal
Where Rust shinesEverywhere you want speed and safety
• Operating Systems • Compilers • Emulators
Encryption • Databases
Numerics • Graphics Programming • Data Pipelines
Finance • …
struct Person { first_name: String, last_name: String, age: i32 }
impl Person { pub fn new(first_name: String, last_name: String, age: i32) -> Person { Person { first_name: first_name, last_name: last_name, age: age } } }
fn main() {
let mut vec = Vec::new(); vec.push(Person::new(String::from("Austin"), String::from("Powers"), 13)); vec.push(Person::new(String::from("Dr."), String::from("Evil"), 30));
for person in &vec { let result = match person.age { 0...18 => "Teenager", 18 => "Old enough to go to war", _ => "Too Old" }; } }
RUST
What I like about…
• PHP: Package manager, Community
• Python: Syntax, Libraries
• Golang: Tooling, Documentation, Concurrency
• C: Speed, no overhead
What I like about…
• PHP: Package manager, Community
• Python: Syntax, Libraries
• Golang: Tooling, Documentation, Concurrency
• C: Speed, no overhead
RUST
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
What sucks about Rust
• Syntax (a bit)
• „Fighting with the Borrow checker“
• Missing packages (you can help!)
• probably more…
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
#include <iostream> #include <vector> #include <string>
int main() { std::vector<std::string> v;
v.push_back("foo"); auto const &x = v[0]; v.push_back("bar");
// Undefined behavior! std::cout << x; }
C++
There’s multiple mutable references to the same data
auto const &x = v[0] v was copied to new location
fn main() { let mut v = Vec::new();
v.push("foo"); let mut x = &mut v[0]; v.push("bar");
println!("{}", x) }
RUST
Rust
- No data races
- No segfaults
- No null pointers
- No race conditions
John Carmack, Creator of Doom and Quake
fn f(x: Type) {...}
fn f(x: &Type) {...}
fn f(x: &mut Type) {...}
Mutable borrow
Ownership
• Share as you like • read-only
• one at a time • read/write
Immutable borrow
• Total control • read-write
IDEs?
Visual Studio
Visual Studio Code using Rusty Code plugin
Atom with language-rust plugin
Some common cargo commands are: build Compile the current project doc Build this project's and its dependencies' documentation new Create a new cargo project run Build and execute src/main.rs test Run the tests bench Run the benchmarks update Update dependencies listed in Cargo.lock search Search registry for crates publish Package and upload this project to the registry install Install a Rust binary
Cargothe Rust package manager
Updating registry `https://github.com/rust-lang/crates.io-index`simple_parallel (0.3.0) Straight-forward functions and types for basic data parallel operations, including parallel maps, for loops and thread pools.forkjoin (2.3.0) A work stealing fork-join parallelism library for Rustarrayfire (3.2.0) ArrayFire is a high performance software library for parallel computing with an easy-to-use API. Its array based function set m…parry (0.1.0) Parallel array processing: deflect performance problems.mpi (0.2.0) Message Passing Interface bindings for Rustspecs (0.6.1) Parallel Entity-Component System. Specs is a parallel ECS in Rust. It combines the performance of the beast with the flexibili…collenchyma (0.0.8) high-performance computation on any hardwaresubstudy (0.4.0) Language-learning tools for working with parallel, bilingual subtitles and media files.crossbeam (0.2.9) Support for lock-free data structures, synchronizers, and parallel programmingabc (0.2.3) An implementation of Karaboga's Artificial Bee Colony algorithm.... and 27 crates more (use --limit N to see more)
››› cargo search parallel
My tool wishlistUnit testing: cargo test
Package manager
Syntax highlighting
Formatting
Code completion
Debugging
Code linter
Benchmarking
Profiling
Code coverage
https://doc.rust-lang.org/book/testing.html
Unit testing
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting
Formatting
Code completion
Debugging
Code linter
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting
Code completion
Debugging
Code linter
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion
Debugging
Code linter
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging
Code linter
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter: clippy
Benchmarking
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter: clippy
Benchmarking: cargo bench
Profiling
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter: clippy
Benchmarking: cargo bench
Profiling: meh…
Code coverage
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter: clippy
Benchmarking: cargo bench
Profiling: meh… torch?
Code coverage
https://llogiq.github.io/2015/07/15/profiling.html
Calls
Time
My tool wishlistUnit testing: cargo test
Package manager: cargo build/run/install
Syntax highlighting: many plugins
Formatting: rustfmt
Code completion: racer
Debugging: rust gdb?
Code linter: clippy
Benchmarking: cargo bench
Profiling: meh… torch?
Code coverage: kcov?
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:
SummaryLots of work to do
But impressive start
We need to focus on those…
• Debugging
• Profiling
• Code Coverage
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 secureRedox
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
Bonusrr - a reverse debugger (http://rr-project.org/)
Dash - Offline API Documentation browser for Mac (https://kapeli.com/dash)
Crates.io reverse package lookup (https://crates.io/crates/serde/reverse_dependencies)
www.meetup.com/Rust-Amsterdam
https://users.rust-lang.org/
Even more athttps://gist.github.com/nrc/a3bbf6dd1b14ce57f18c
http://kukuruku.co/hub/rust/comparing-rust-and-cpp