$30 off During Our Annual Pro Sale. View Details »

What is Rust doing behind the curtains?

What is Rust doing behind the curtains?

This is an hands-on talk, showing a journey from code with a lot of syntactic sugar to plain, veeery explicit Rust code.

Rust allows for a lot of syntactic sugar, that makes it a pleasure to write. It is sometimes hard, however, to look behind the curtain and see what the compiler is really doing with our code. "It is good to know what these conveniences are, to avoid being mystified by what's going on under the hood... the less magical thinking we have of the world, the better." (Tshepang Lekhonkhobe). In this hands-on talk, we will go from code with a lot of syntactic sugar to extremely verbose Rust code.

We will use a little tool called `cargo-inspect`, which was built for teaching Rust internals. The goal is to make the compiler more approachable to mere mortals.

Matthias Endler

February 03, 2019
Tweet

More Decks by Matthias Endler

Other Decks in Programming

Transcript

  1. What Is Rust Doing
    Behind the Curtains?

    View Slide

  2. Matthias Endler
    !2
    Hi! I am
    You might know me from...
    My YouTube channel! My Blog! Not at all!

    View Slide

  3. • Hotel Search Platform
    • 2.5m+ Hotels/Accommodations
    • IT departments in Düsseldorf, Leipzig, Palma, Amsterdam
    • Java, Kotlin, Go, PHP, Python (, Rust?)
    • tech.trivago.com

    View Slide

  4. Why should I care?
    !4

    View Slide

  5. Rust is a systems programming language that runs
    blazingly fast, prevents segfaults, and guarantees thread
    safety. It aims to bring modern language design and an
    advanced type system to systems programming. Rust
    does not use a garbage collector, using advanced static
    analysis to provide deterministic drops instead.

    View Slide

  6. Rust is a systems programming language that runs
    blazingly fast, prevents segfaults, and guarantees thread
    safety. It aims to bring modern language design and an
    advanced type system to systems programming. Rust
    does not use a garbage collector, using advanced static
    analysis to provide deterministic drops instead.

    View Slide

  7. Empowering everyone
    to build reliable and
    efficient software.

    View Slide

  8. Be curious.
    Try crazy things.
    Don't be afraid.

    View Slide

  9. !9

    View Slide

  10. Being curious is an amazing trait!
    We should embrace it,
    and help people be curious.
    Pascal Hertleif –

    Rust’s approach of getting things right

    View Slide

  11. Julia Evans

    View Slide

  12. Why's Poignant Guide to Ruby

    View Slide

  13. View Slide

  14. The Rust Compiler
    !14

    View Slide

  15. !15
    RUST SOURCE
    HIR
    Parsing

    Desugaring
    MIR
    Borrow-checking

    Optimization
    LLVM IR
    Optimization
    MACHINE CODE
    Optimization

    View Slide

  16. !16
    RUST SOURCE
    HIR
    Parsing

    Desugaring
    MIR
    Optimization
    LLVM IR
    MACHINE CODE
    Optimization
    Borrow-checking

    Optimization
    HAIR
    Optimization

    View Slide

  17. Desugaring...
    Candy designed by Freepik, Vegetables by Macrovector

    View Slide

  18. Code examples!
    At last...
    !18

    View Slide

  19. Example1

    View Slide

  20. fn main() {}

    View Slide

  21. #[macro_use]
    extern crate std;
    #[prelude_import]
    use std::prelude::v1::*;
    fn main() {}

    View Slide

  22. std::boxed::Box
    std::option::Option::{self, Some, None}
    std::result::Result::{self, Ok, Err}
    std::string::String;
    std::vec::Vec
    std::borrow::ToOwned
    std::clone::Clone
    std::cmp::{PartialEq, PartialOrd, Eq, Ord }
    std::convert::{AsRef, AsMut, Into, From}
    std::default::Default
    std::iter::{DoubleEndedIterator, ExactSizeIterator}
    std::iter::{Iterator, Extend, IntoIterator}
    std::marker::{Copy, Send, Sized, Sync}
    std::ops::{Drop, Fn, FnMut, FnOnce}
    std::slice::SliceConcatExt
    std::string::ToString
    std::mem::drop
    Types:
    Traits:
    Functions:

    View Slide

  23. Box, Option, Result, String, Vec
    PartialEq, PartialOrd, Eq, Ord

    AsRef, AsMut, Into, From, ToOwned, Clone, ToString

    Default

    DoubleEndedIterator, ExactSizeIterator
    Iterator, Extend, IntoIterator

    Copy, Send, Sized, Sync

    Drop, Fn, FnMut, FnOnce

    SliceConcatExt
    Types:
    Traits: Ordering things
    Converting things
    Default values
    Marker traits
    Calling/Dropping objects
    Concatenate objects

    (like strings or vectors)
    Iteration

    View Slide

  24. Example2
    Ranges

    View Slide

  25. for i in 0..3 {
    // do something with i
    }

    View Slide

  26. let range = 0..3;
    for i in range {
    // do something with i
    }

    View Slide

  27. let range = Range {0, 3};
    for i in range {
    // do something with i
    }

    View Slide

  28. let range = Range {0, 3};
    for i in range {
    // do something with i
    }

    View Slide

  29. use std::ops::Range;
    let range = Range { start: 0, end: 3 };
    for i in range {
    // do something with i
    }

    View Slide

  30. use std::iter::IntoIterator;

    use std::ops::Range;
    let range = Range { start: 0, end: 3 };
    let mut iter =
    IntoIterator::into_iter(range);
    while let Some(i) = iter.next() {
    // do something with i
    }

    View Slide

  31. use std::iter::IntoIterator;
    use std::ops::Range;
    let range = Range { start: 0, end: 3 };
    let mut iter =
    IntoIterator::into_iter(range);
    loop {
    match iter.next() {
    Some(i) => { /* use i */ },
    None => break,
    }
    }

    View Slide

  32. cargo inspect

    View Slide

  33. !33
    cargo-install cargo-inspect

    cargo inspect foo.rs

    View Slide

  34. Example3
    Ranges - Part II

    View Slide

  35. for i in 0..=3 {
    // do something with i
    }

    View Slide

  36. use std::iter::IntoIterator;
    use std::ops::RangeInclusive;
    let range = RangeInclusive::new(0, 3);
    let mut iter =
    IntoIterator::into_iter(range);
    loop {
    match iter.next() {
    Some(i) => { /* use i */ },
    None => break,
    }
    }

    View Slide

  37. cargo inspect --diff foo.rs,bar.rs
    !37

    View Slide

  38. Example4
    Opening Files

    View Slide

  39. use std::fs::File;
    use std::io::Error;
    fn main() -> Result<(), Error> {
    let f = File::open("file.txt")?;
    Ok(())
    }

    View Slide

  40. use std::fs::File;
    use std::io::Error;
    fn main() -> Result<(), Error> {
    let f = match File::open("file.txt") {
    Ok(file) => file,
    Err(err) => return Err(err),
    };
    Ok(())
    }

    View Slide

  41. use std::fs::File;
    use std::io::Error;
    use std::convert::From;
    fn main() -> Result<(), Error> {
    let f = match File::open("file.txt") {
    Ok(file) => file,
    Err(err) => return Err(From::from(err)),
    };
    Ok(())
    }

    View Slide

  42. cargo-inspect-vscode

    View Slide

  43. More cargo tools!
    !43
    • cargo-expand
    • cargo-asm
    • cargo-bloat

    View Slide

  44. Rust Playground
    !44

    View Slide

  45. !45
    play.rust-lang.org

    View Slide

  46. Compiler Explorer
    !46

    View Slide

  47. !47
    godbolt.org

    View Slide

  48. Lessons Learned
    • Rust allows for lots of syntactic sugar
    • It's good to be reminded about that sometimes
    • Tools help us understand what's going on
    behind the curtains.
    !48

    View Slide

  49. !49
    Now go and build cool things!

    View Slide

  50. View Slide

  51. Credits
    •Stage background from freepik.com designed by
    starline
    •Lucy with a Rocket engine
    •Rustlang MIR documentation
    •Rust compiler guide
    !51

    View Slide