What Is Rust Doing Behind the Curtains? · 2019-04-15 · Rust is a systems programming language...

51
What Is Rust Doing Behind the Curtains?

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

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

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

• 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

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

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

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

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

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

!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

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 12: 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

Why's Poignant Guide to Ruby

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

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

!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

!16

RUST SOURCE

HIR

ParsingDesugaring

MIR

Optimization

LLVM IR

MACHINE CODEOptimization

Borrow-checkingOptimization

HAIROptimization

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

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

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

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

#[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

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

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

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

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

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

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

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

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

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

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

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

!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

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

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

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

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

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

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

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

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 42: 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

cargo-inspect-vscode

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

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

Rust Playground

!44

Page 45: 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

!45

play.rust-lang.org

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

Compiler Explorer

!46

Page 47: 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

!47

godbolt.org

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

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

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

Credits•Stage background from freepik.com designed by starline

•Lucy with a Rocket engine •Rustlang MIR documentation •Rust compiler guide

!51