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.

Ed9e9992fe069a0d3de05b69d8d187c3?s=128

Matthias Endler

February 03, 2019
Tweet

Transcript

  1. What Is Rust Doing Behind the Curtains?

  2. Matthias Endler !2 Hi! I am You might know me

    from... My YouTube channel! My Blog! Not at all!
  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
  4. Why should I care? !4

  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.
  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.
  7. Empowering everyone to build reliable and efficient software.

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

  9. !9

  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
  11. Julia Evans

  12. Why's Poignant Guide to Ruby

  13. None
  14. The Rust Compiler !14

  15. !15 RUST SOURCE HIR Parsing
 Desugaring MIR Borrow-checking
 Optimization LLVM

    IR Optimization MACHINE CODE Optimization
  16. !16 RUST SOURCE HIR Parsing
 Desugaring MIR Optimization LLVM IR

    MACHINE CODE Optimization Borrow-checking
 Optimization HAIR Optimization
  17. Desugaring... Candy designed by Freepik, Vegetables by Macrovector

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

  19. Example1

  20. fn main() {}

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

  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:
  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
  24. Example2 Ranges

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

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

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

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

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

    3 }; for i in range { // do something with i }
  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 }
  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, } }
  32. cargo inspect

  33. !33 cargo-install cargo-inspect
 cargo inspect foo.rs

  34. Example3 Ranges - Part II

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

    }
  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, } }
  37. cargo inspect --diff foo.rs,bar.rs !37

  38. Example4 Opening Files

  39. use std::fs::File; use std::io::Error; fn main() -> Result<(), Error> {

    let f = File::open("file.txt")?; Ok(()) }
  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(()) }
  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(()) }
  42. cargo-inspect-vscode

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

  44. Rust Playground !44

  45. !45 play.rust-lang.org

  46. Compiler Explorer !46

  47. !47 godbolt.org

  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
  49. !49 Now go and build cool things!

  50. None
  51. Credits •Stage background from freepik.com designed by starline •Lucy with

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