What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language...
Transcript of What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language...
![Page 1: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/1.jpg)
What Is Rust Doing Behind the Curtains?
![Page 2: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/2.jpg)
Matthias Endler
!2
Hi! I am
You might know me from...
My YouTube channel! My Blog! Not at all!
![Page 3: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/3.jpg)
• Hotel Search Platform
• 2.5m+ Hotels/Accommodations
• IT departments in Düsseldorf, Leipzig, Palma, Amsterdam
• Java, Kotlin, Go, PHP, Python (, Rust?)
• tech.trivago.com
![Page 4: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/4.jpg)
Why should I care?
!4
![Page 5: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/5.jpg)
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.
![Page 6: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/6.jpg)
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.
![Page 7: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/7.jpg)
Empowering everyone to build reliable and efficient software.
![Page 8: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/8.jpg)
Be curious. Try crazy things. Don't be afraid.
![Page 9: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/9.jpg)
!9
![Page 10: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/10.jpg)
Being curious is an amazing trait! We should embrace it, and help people be curious.Pascal Hertleif –Rust’s approach of getting things right
![Page 13: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/13.jpg)
![Page 14: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/14.jpg)
The Rust Compiler
!14
![Page 15: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/15.jpg)
!15
RUST SOURCE
HIR
ParsingDesugaring
MIR
Borrow-checkingOptimization
LLVM IROptimization
MACHINE CODEOptimization
![Page 16: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/16.jpg)
!16
RUST SOURCE
HIR
ParsingDesugaring
MIR
Optimization
LLVM IR
MACHINE CODEOptimization
Borrow-checkingOptimization
HAIROptimization
![Page 17: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/17.jpg)
Desugaring...
Candy designed by Freepik, Vegetables by Macrovector
![Page 18: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/18.jpg)
Code examples!At last...
!18
![Page 19: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/19.jpg)
Example1
![Page 20: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/20.jpg)
fn main() {}
![Page 21: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/21.jpg)
#[macro_use] extern crate std;
#[prelude_import] use std::prelude::v1::*;
fn main() {}
![Page 22: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/22.jpg)
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:
![Page 23: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/23.jpg)
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 thingsDefault values
Marker traitsCalling/Dropping objectsConcatenate objects(like strings or vectors)
Iteration
![Page 24: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/24.jpg)
Example2Ranges
![Page 25: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/25.jpg)
for i in 0..3 { // do something with i }
![Page 26: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/26.jpg)
let range = 0..3;
for i in range { // do something with i }
![Page 27: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/27.jpg)
let range = Range {0, 3};
for i in range { // do something with i }
![Page 28: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/28.jpg)
let range = Range {0, 3};
for i in range { // do something with i }
![Page 29: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/29.jpg)
use std::ops::Range;
let range = Range { start: 0, end: 3 };
for i in range { // do something with i }
![Page 30: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/30.jpg)
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 }
![Page 31: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/31.jpg)
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, } }
![Page 32: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/32.jpg)
cargo inspect
![Page 33: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/33.jpg)
!33
cargo-install cargo-inspectcargo inspect foo.rs
![Page 34: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/34.jpg)
Example3Ranges - Part II
![Page 35: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/35.jpg)
for i in 0..=3 { // do something with i }
![Page 36: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/36.jpg)
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, } }
![Page 37: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/37.jpg)
cargo inspect --diff foo.rs,bar.rs
!37
![Page 38: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/38.jpg)
Example4Opening Files
![Page 39: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/39.jpg)
use std::fs::File; use std::io::Error;
fn main() -> Result<(), Error> { let f = File::open("file.txt")?; Ok(()) }
![Page 40: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/40.jpg)
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(()) }
![Page 41: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/41.jpg)
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(()) }
![Page 43: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/43.jpg)
More cargo tools!
!43
• cargo-expand • cargo-asm • cargo-bloat
![Page 44: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/44.jpg)
Rust Playground
!44
![Page 46: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/46.jpg)
Compiler Explorer
!46
![Page 48: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/48.jpg)
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
![Page 49: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/49.jpg)
!49
Now go and build cool things!
![Page 50: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/50.jpg)
![Page 51: What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. It aims](https://reader031.fdocuments.us/reader031/viewer/2022041022/5ed2ddc3a079355bb26da179/html5/thumbnails/51.jpg)
Credits•Stage background from freepik.com designed by starline
•Lucy with a Rocket engine •Rustlang MIR documentation •Rust compiler guide
!51