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.

Ed9e9992fe069a0d3de05b69d8d187c3?s=128

Matthias Endler

October 05, 2017
Tweet

Transcript

  1. MY GOOD FRIEND RUST Matthias Endler trivago

  2. How to write the word Mississippi?

  3. Mississippi Sebastian is stupid. How to write the word Mississippi?

  4. None
  5. None
  6. None
  7. None
  8. Oh please tell me more about Matthias Endler! } Düsseldorf,

    Germany } Backend Engineer at } Website performance } Hot Chocolate @matthiasendler mre https://matthias-endler.de
  9. 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
  10. http://doom.wikia.com/wiki/John_Carmack

  11. 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
  12. Web Low-level Functional

  13. PHP
 Python Ruby C C++ Clojure Haskell

  14. C C++ Clojure Haskell Safety,
 Rapid prototyping

  15. Clojure Haskell Safety,
 Rapid prototyping Total control,
 Performance

  16. Safety,
 Rapid prototyping Total control,
 Performance High-level abstractions

  17. Safety,
 Rapid prototyping Total control,
 Performance High-level abstractions Rust

  18. 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
  19. 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
  20. Rust vs Python/Ruby/PHP - Performance https://blog.sentry.io/2016/10/19/fixing-python-performance-with-rust.html

  21. 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
  22. 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)
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. Python Rust Rust parallel Runtime (seconds) Rust vs Python/Ruby/PHP -

    Concurrency
  29. } 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++
  30. #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
  31. 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
  32. Rust vs C/C++ - Memory safety

  33. Rust vs C/C++ - Memory safety https://cacm.acm.org/magazines/2017/10/221326-a-large-scale-study-of-programming-languages-and-code-quality-in-github/fulltext

  34. Rust vs C/C++ - Memory safety https://cacm.acm.org/magazines/2017/10/221326-a-large-scale-study-of-programming-languages-and-code-quality-in-github/fulltext

  35. } Zero-cost abstractions } No garbage-collector } No legacy Rust

    vs Haskell/Clojure/Kotlin…
  36. 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
  37. Rust vs Haskell/Clojure/Kotlin… - Laziness foldl (+) 0 [1..1000000::Integer] https://wiki.haskell.org/Memory_leak#Building_up_unevaluated_expressions

    (1..1000000).sum() Haskell Rust
  38. Safety,
 Rapid prototyping Total control,
 Performance High-level abstractions

  39. } 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
  40. Rust improvement areas Understanding the borrow checker Difficulty Time

  41. Who’s using Rust anyway? Background image by Neil Howard ((https://flic.kr/p/nSSuUd)

  42. } 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?
  43. None
  44. Rust Community

  45. Swear words in Reddit comments per programming language Source: https://github.com/Dobiasd/programming-language-subreddits-and-their-choice-of-words

  46. 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%
  47. Most loved language according to Stack Overflow 2017 Rust Smalltalk

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

    They all just happen to use the same programming language.
  49. Matthias Endler matthias-endler.de
 trivago matthias.endler@trivago.com Rate my talk please <3


    Thanks to Thomas Künneth for lending me the clicker!