why rust? - matthias endler - codemotion amsterdam 2016

51
Why Rust?

Upload: codemotion

Post on 14-Apr-2017

281 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Why Rust?

Page 2: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

- Düsseldorf, Germany

- Backend Engineer at trivago

- Website performance team

- Worked a lot with Python and PHP

- Likes hot chocolate

@matthiasendler

Matthias Endlermre

Page 3: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

C

me

C++

Page 4: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016
Page 5: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

System Programming

Memory management

Error handling

Static Typing

Compiling

Page 6: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Rust

- Created by Graydon Hoare

- Funded by Mozilla

- First version: 2010

- Current stable version 1.8

A safe systems language

Page 7: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

System Programmingis challenging!

Page 8: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

• Provide the right abstractions

• Provide awesome tools

• Allow making mistakes

• Build a helpful community

System Programming

can be fun!

Page 9: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Complexity vs Speed• PHP

• Python

• Golang

• Rust

• C

Faster „Easier“

Page 10: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

RUST: High level on bare metal

Page 11: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Where Rust shinesEverywhere you want speed and safety

• Operating Systems • Compilers • Emulators

Encryption • Databases

Numerics • Graphics Programming • Data Pipelines

Finance • …

Page 12: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 13: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

What I like about…

• PHP: Package manager, Community

• Python: Syntax, Libraries

• Golang: Tooling, Documentation, Concurrency

• C: Speed, no overhead

Page 14: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

What I like about…

• PHP: Package manager, Community

• Python: Syntax, Libraries

• Golang: Tooling, Documentation, Concurrency

• C: Speed, no overhead

RUST

Page 15: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 16: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

What sucks about Rust

• Syntax (a bit)

• „Fighting with the Borrow checker“

• Missing packages (you can help!)

• probably more…

Page 17: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 18: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

#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++

Page 19: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

There’s multiple mutable references to the same data

auto const &x = v[0] v was copied to new location

Page 20: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

fn main() { let mut v = Vec::new();

v.push("foo"); let mut x = &mut v[0]; v.push("bar");

println!("{}", x) }

RUST

Page 21: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Rust

- No data races

- No segfaults

- No null pointers

- No race conditions

Page 22: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

John Carmack, Creator of Doom and Quake

Page 23: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 24: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

IDEs?

Page 25: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Visual Studio

Page 26: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Visual Studio Code using Rusty Code plugin

Page 27: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Atom with language-rust plugin

Page 28: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 29: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 30: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

My tool wishlistUnit testing: cargo test

Package manager

Syntax highlighting

Formatting

Code completion

Debugging

Code linter

Benchmarking

Profiling

Code coverage

Page 31: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Unit testing

Page 32: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

My tool wishlistUnit testing: cargo test

Package manager: cargo build/run/install

Syntax highlighting

Formatting

Code completion

Debugging

Code linter

Benchmarking

Profiling

Code coverage

Page 33: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 34: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 35: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 36: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 37: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 38: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 39: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 40: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 41: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Calls

Time

Page 42: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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?

Page 43: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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:

Page 44: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

SummaryLots of work to do

But impressive start

Page 45: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

We need to focus on those…

• Debugging

• Profiling

• Code Coverage

Page 46: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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

Page 47: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

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)

Page 48: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

www.meetup.com/Rust-Amsterdam

Page 49: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

https://users.rust-lang.org/

Page 50: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016

Even more athttps://gist.github.com/nrc/a3bbf6dd1b14ce57f18c

http://kukuruku.co/hub/rust/comparing-rust-and-cpp

Page 51: Why Rust? - Matthias Endler - Codemotion Amsterdam 2016