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

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. Matthias Endler !2 Hi! I am You might know me

    from... My YouTube channel! My Blog! Not at all!
  2. • Hotel Search Platform • 2.5m+ Hotels/Accommodations • IT departments

    in Düsseldorf, Leipzig, Palma, Amsterdam • Java, Kotlin, Go, PHP, Python (, Rust?) • tech.trivago.com
  3. 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.
  4. 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.
  5. !9

  6. Being curious is an amazing trait! We should embrace it,

    and help people be curious. Pascal Hertleif –
 Rust’s approach of getting things right
  7. !16 RUST SOURCE HIR Parsing
 Desugaring MIR Optimization LLVM IR

    MACHINE CODE Optimization Borrow-checking
 Optimization HAIR Optimization
  8. 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:
  9. 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
  10. let range = Range {0, 3}; for i in range

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

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

    3 }; for i in range { // do something with i }
  13. 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 }
  14. 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, } }
  15. 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, } }
  16. 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(()) }
  17. 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(()) }
  18. 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
  19. Credits •Stage background from freepik.com designed by starline •Lucy with

    a Rocket engine •Rustlang MIR documentation •Rust compiler guide !51