Upgrade to Pro — share decks privately, control downloads, hide ads and more …

My Good Friend Rust - Topconf 2017

My Good Friend Rust - Topconf 2017

Rust is more than a Systems Programming language; it's a new way of thinking about low-level code.· It has strong safety and concurrency guarantees without sacrificing performance. It provides powerful abstractions that make Systems Programming both, easier and more fun.
Let me show you what's in for you and why you should learn Rust, even though you might be a Web-Developer. The growing ecosystem of Rust libraries and the steadily improving IDE support will help a lot in getting started. I'll show you some practical code examples, tell you how trivago uses Rust and give an overview of the road ahead.

Matthias Endler

October 05, 2017
Tweet

More Decks by Matthias Endler

Other Decks in Programming

Transcript

  1. Oh please tell me more about Matthias Endler! } Düsseldorf,

    Germany } Backend Engineer at } Website performance } Hot Chocolate @matthiasendler mre https://matthias-endler.de
  2. Programming is easy! People pay me for that!? What’s a

    void pointer? Ooooh… What if programming was no fun? There is no god. Code is beautiful. Here’s a lollipop. Meh… I know nothing about programming. That’s okay. What I learned from other languages BASIC PHP C Java C++ Python Ruby Scala Haskell Go
  3. Logo by johndory from http://www.freepik.com/free-vector/crabs-pattern-design_1093131.htm Funded by Mozilla First version:

    2010 Version 1.0: May 2015 Current stable version: 1.20 High-level on bare metal Rust
  4. Rust vs Python/Ruby/PHP } Better Performance } Lower memory footprint

    } Concurrency without overhead } Type system } Easily embeddable into existing projects } Minimal runtime } No garbage-collector
  5. Rust vs Python/Ruby/PHP } Better Performance } Lower memory footprint

    } Concurrency without overhead } Type system } Easily embeddable into existing projects } Minimal runtime } No garbage-collector
  6. Rust vs Python/Ruby/PHP - Type System def quicksort(values): if not

    values: return [] else: pivot = values[0] less = [x for x in values if x < pivot] more = [x for x in values[1:] if x >= pivot] return quicksort(less) + [pivot] + quicksort(more) print(quicksort([4, 2, 1, 5])) # [1, 2, 4, 5] print(quicksort("hello")) # ['e', 'h', 'l', 'l', 'o'] print(quicksort(-1)) Traceback (most recent call last): File "sort.py", line 12, in <module> print(quicksort(-1)) File "sort.py", line 5, in quicksort pivot = values[0] TypeError: 'int' object is not subscriptable
  7. fn quicksort<T: PartialOrd + Copy>(values: Vec<T>) -> Vec<T> { let

    pivot = match values.get(0) { Some(v) => v, None => return vec![], }; let less = values.iter().filter(|&e| e < pivot).map(|&e| e).collect(); let more = values.iter().filter(|&e| e > pivot).map(|&e| e).collect(); [quicksort(less), vec![*pivot], quicksort(more)].concat() } Rust vs Python/Ruby/PHP - Syntax def quicksort(values): if not values: return [] else: pivot = values[0] less = [x for x in values if x < pivot] more = [x for x in values[1:] if x >= pivot] return quicksort(less) + [pivot] + quicksort(more)
  8. Rust vs Python/Ruby/PHP - Concurrency prime_map = {n: is_prime(n) for

    n in range(400_000)} Runtime:
 9min 35s def is_prime(n): if n < 2: return False for d in range(2,n): if n % d == 0: return False return True
  9. Rust vs Python/Ruby/PHP - Concurrency Python Rust fn is_prime(n: u64)

    -> bool { match n { 0...1 => false, _ => { for d in 2..n { if n % d == 0 { return false; } } true } } } def is_prime(n): if n < 2: return False for d in range(2,n): if n % d == 0: return False return True
  10. Rust vs Python/Ruby/PHP - Concurrency fn is_prime(n: u64) -> bool

    { match n { 0...1 => false, _ => !(2..n).any(|d| n % d == 0), } } def is_prime(n): if n < 2: return False for d in range(2,n): if n % d == 0: return False return True Python Rust
  11. fn is_prime(n: u64) -> bool { match n { 0...1

    => false, _ => { for d in 2..n { if n % d == 0 { return false; } } true } } } Rust vs Python/Ruby/PHP - Concurrency let prime_map: HashMap<u64, bool> = (1..400_000) .map(|n| (n, is_prime(n))) .collect(); Runtime:
 54 seconds
  12. fn is_prime(n: u64) -> bool { match n { 0...1

    => false, _ => { for d in 2..n { if n % d == 0 { return false; } } true } } } Rust vs Python/Ruby/PHP - Concurrency (using rayon) let prime_map: HashMap<u64, bool> = (1..400_000) .into_par_iter() .map(|n| (n, is_prime(n))) .collect(); Runtime:
 9.6 seconds
  13. } Memory safety } Package manager (cargo) } High-level language

    concepts } Easily embeddable into existing projects } Fearless concurrency } Strong compiler checks } No undefined behavior Rust vs C/C++
  14. #include <stdio.h> int *get_num(void) { int num = 1234; return

    &num; } int main() { int* num = get_num(); printf("%d", *num); } fn get_num() -> &i64 { let num = 1234; return &num; } fn main() { let num = get_num(); println!("{}", *num); } C Rust warning, but will execute error, will not execute Rust vs C/C++ - Memory safety
  15. Rust error message: Ownership of your data must be clear

    at all times (No worries, the compiler will help you with that) Rust vs C/C++ - Memory safety
  16. Rust vs Haskell/Clojure/Kotlin… - Zero cost abstractions fn print_hash<T: Hash>(t:

    &T) { println!("The hash is {}", t.hash()) } print_hash(&true); // instantiates T = bool print_hash(&12_i64); // instantiates T = i64 // The compiled code: __print_hash_bool(&true); // invoke specialized bool version directly __print_hash_i64(&12_i64); // invoke specialized i64 version directly
  17. } Syntax (a bit) } Documentation/Books/Workshops } Missing tooling (IDEs,

    Debuggers,…) } Missing libs (help out!) } Many core libs are still < 1.0 } Steep learning curve (Fighting with the borrow checker) } SIMD support } … Rust improvement areas
  18. } Firefox } Dropbox } Visual Studio Code } npm

    } Facebook (kind of) } Google (kind of) } https://www.rust-lang.org/en-US/friends.html Who’s using Rust anyway?
  19. Most loved language according to Stack Overflow 2016 Rust Swift

    F# Scala Go Clojure React Haskell Python C# Node.js 62.5% 62.0% 59.6%
  20. Most loved language according to Stack Overflow 2017 Rust Smalltalk

    TypeScript Swift Go Python Elixir C# Scala Clojure JavaScript
  21. Rust is an incredible community of people who ❤ programming.

    They all just happen to use the same programming language.