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. MY GOOD FRIEND RUST
    Matthias Endler
    trivago

    View Slide

  2. How to write the word Mississippi?

    View Slide

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

    View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  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

    View Slide

  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

    View Slide

  10. http://doom.wikia.com/wiki/John_Carmack

    View Slide

  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

    View Slide

  12. Web
    Low-level Functional

    View Slide

  13. PHP

    Python
    Ruby
    C
    C++
    Clojure
    Haskell

    View Slide

  14. C
    C++
    Clojure
    Haskell
    Safety,

    Rapid
    prototyping

    View Slide

  15. Clojure
    Haskell
    Safety,

    Rapid
    prototyping
    Total control,

    Performance

    View Slide

  16. Safety,

    Rapid
    prototyping
    Total control,

    Performance
    High-level
    abstractions

    View Slide

  17. Safety,

    Rapid
    prototyping
    Total control,

    Performance
    High-level
    abstractions
    Rust

    View Slide

  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

    View Slide

  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

    View Slide

  20. Rust vs Python/Ruby/PHP - Performance
    https://blog.sentry.io/2016/10/19/fixing-python-performance-with-rust.html

    View Slide

  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
    print(quicksort(-1))
    File "sort.py", line 5, in quicksort
    pivot = values[0]
    TypeError: 'int' object is not subscriptable

    View Slide

  22. fn quicksort(values: Vec) -> Vec {
    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)

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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 =
    (1..400_000)
    .map(|n| (n, is_prime(n)))
    .collect();
    Runtime:

    54 seconds

    View Slide

  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 =
    (1..400_000)
    .into_par_iter()
    .map(|n| (n, is_prime(n)))
    .collect();
    Runtime:

    9.6 seconds

    View Slide

  28. Python Rust Rust parallel
    Runtime (seconds)
    Rust vs Python/Ruby/PHP - Concurrency

    View Slide

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

    View Slide

  30. #include
    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

    View Slide

  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

    View Slide

  32. Rust vs C/C++ - Memory safety

    View Slide

  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

    View Slide

  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

    View Slide

  35. } Zero-cost abstractions
    } No garbage-collector
    } No legacy
    Rust vs Haskell/Clojure/Kotlin…

    View Slide

  36. Rust vs Haskell/Clojure/Kotlin… - Zero cost abstractions
    fn print_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

    View Slide

  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


    View Slide

  38. Safety,

    Rapid
    prototyping
    Total control,

    Performance
    High-level
    abstractions

    View Slide

  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

    View Slide

  40. Rust improvement areas
    Understanding the borrow checker
    Difficulty
    Time

    View Slide

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

    View Slide

  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?

    View Slide

  43. View Slide

  44. Rust Community

    View Slide

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

    View Slide

  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%

    View Slide

  47. Most loved language according to Stack Overflow 2017
    Rust
    Smalltalk
    TypeScript
    Swift
    Go
    Python
    Elixir
    C#
    Scala
    Clojure
    JavaScript

    View Slide

  48. Rust is an incredible community
    of people
    who ❤ programming.
    They all just happen to use the
    same programming language.

    View Slide

  49. Matthias Endler matthias-endler.de

    trivago [email protected]
    Rate my talk please <3

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

    View Slide