Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik....

100
Rust’s Journey to Async/Await Steve Klabnik

Transcript of Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik....

Page 1: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust’s Journey to Async/AwaitSteve Klabnik

Page 2: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Hi, I’m Steve!

● On the Rust team

● Work at Cloudflare

● Doing two workshops!

Page 3: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 4: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

What is async?

Parallel: do multiple things at once

Concurrent: do multiple things, not at once

Asynchronous: actually unrelated! Sort of...

Page 5: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

“Task”A generic term for some computation running in a parallel or concurrent system

Page 6: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

ParallelOnly possible with multiple

cores or CPUs

Page 7: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

ConcurrentPretend that you have multiple

cores or CPUs

Page 8: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

AsynchronousA word we use to describe language features that enable parallelism and/or concurrency

Page 9: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Even more terminology

Page 10: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Cooperative vs Preemptive Multitasking

Page 11: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Cooperative Multitasking Each task decides when to

yield to other tasks

Page 12: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Preemptive Multitasking The system decides when to

yield to other tasks

Page 13: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Native vs green threads

Page 14: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Native threads Tasks provided by the operating system

Sometimes called “1:1 threading”

Page 15: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Green Threads Tasks provided by your programming language

Sometimes called “N:M threading”

Page 16: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Native vs Green threads

Native thread advantages:

● Part of your system; OS handles scheduling

● Very straightforward, well-understood

Native thread disadvantages:

● Defaults can be sort of heavy● Relatively limited number you can

create

Green thread advantages:

● Not part of the overall system; runtime handles scheduling

● Lighter weight, can create many, many, many, many green threads

Green thread disadvantages:

● Stack growth can cause issues● Overhead when calling into C

Page 17: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Why do we care?

Page 18: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 19: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Apache“Pre-fork”

Control Process

Child Process

Page 20: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Apache“worker”

Control Process

Child Process

Child Thread Child Thread

Child Thread Child Thread

Thread pool

Page 21: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Let’s talk about Rust

Page 22: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust was built to enhance Firefox, which is an HTTP client, not server

Page 23: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 24: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 25: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

“Synchronous, non-blocking network I/O”

Page 26: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Isn’t this a contradiction in terms?

Page 27: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Synchronous Asynchronous

Blocking Old-school implementations Doesn’t make sense

Non-blocking Go, Ruby Node.js

Page 28: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Tons of options

Synchronous, blocking

● Your code looks like it blocks, and it does block

● Very basic and straightforward

Asynchronous, non-blocking

● Your code looks like it doesn’t block, and it doesn’t block

● Harder to write

Synchronous, non-blocking

● Your code looks like it blocks, but it doesn’t!

● The secret: the runtime is non-blocking

● Your code still looks straightforward, but you get performance benefits

● A common path for languages built on synchronous, blocking I/O to gain performance while retaining compatibility

Page 29: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Not all was well in Rust-land

Page 30: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 31: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

A “systems programming language” that doesn’t let you use the system’s threads?

Page 32: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 33: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 34: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 35: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Not all was well in Rust-land

Page 36: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 37: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust 1.0 was approaching

Page 38: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Ship the minimal thing that we know is good

Page 39: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust 1.0 was released! 🎉

Page 40: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

… but still, not all was well in Rust-land

Page 41: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

People 💖 Rust

Page 42: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

People want to build network services in Rust

Page 43: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust is supposed to be a high-performance language

Page 44: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust’s I/O model feels retro, and not performant

Page 45: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

The big problem with native threads for I/O

Page 46: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

CPU bound vsI/O bound

Page 47: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

CPU BoundThe speed of completing a task is based on the CPU crunching some numbers

My processor is working hard

Page 48: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

I/O BoundThe speed of completing a task is based on doing a lot of input and output

Doing a lot of networking

Page 49: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

When you’re doing a lot of I/O, you’re doing a lot of waiting

Page 50: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

When you’re doing a lot of waiting, you’re tying up system resources

Page 51: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

GoAsynchronous I/O

with green threads

(Erlang does this too)

Main Process

Child Thread Child Thread

Child Thread Child Thread

Green threads

Page 52: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Native vs Green threads

Native thread advantages:

● Part of your system; OS handles scheduling

● Very straightforward, well-understood

Native thread disadvantages:

● Defaults can be sort of heavy● Relatively limited number you can

create

Green thread advantages:

● Not part of the overall system; runtime handles scheduling

● Lighter weight, can create many, many, many, many green threads

Green thread disadvantages:

● Stack growth can cause issues● Overhead when calling into C

PREVIOUSLY

Page 53: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

A “systems programming language” that has overhead when calling into C code?

Page 54: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Luckily, there isanother way

Page 55: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

NginxAsynchronous I/O

Event Loop

Page 56: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 57: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Evented I/O requires non-blocking APIs

Page 58: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Blocking vs non-blocking

Page 59: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

“Callback hell”

Page 60: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 61: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 62: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Promises

let myFirstPromise = new Promise((resolve, reject) => { setTimeout(function(){ resolve("Success!"); }, 250);});

myFirstPromise.then((successMessage) => { console.log("Yay! " + successMessage);});

Page 63: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Promises

let myFirstPromise = new Promise((resolve, reject) => { setTimeout(function(){ resolve("Success!"); }, 250);});

myFirstPromise.then((successMessage) => { console.log("Yay! " + successMessage);}).then((...) => { //}).then((...) => { //});

Page 64: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 65: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 66: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Futures 0.1

pub trait Future { type Item; type Error; fn poll(&mut self) -> Poll<Self::Item, Self::Error>;}

id_rpc(&my_server).and_then(|id| { get_row(id)}).map(|row| { json::encode(row)}).and_then(|encoded| { write_string(my_socket, encoded)})

Page 67: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Promises and Futures are different!

● Promises are built into JavaScript● The language has a runtime● This means that Promises start

executing upon creation● This feels simpler, but has some

drawbacks, namely, lots of allocations

● Futures are not built into Rust● The language has no runtime● This means that you must submit

your futures to an executor to start execution

● Futures are inert until their poll method is called by the executor

● This is slightly more complex, but extremely efficient; a single, perfectly sized allocation per task!

● Compiles into the state machine you’d write by hand with evented I/O

Page 68: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Futures 0.1: Executors

use tokio;

fn main() { let addr = "127.0.0.1:6142".parse().unwrap(); let listener = TcpListener::bind(&addr).unwrap();

let server = listener.incoming().for_each(|socket| { Ok(()) }) .map_err(|err| { println!("accept error = {:?}", err); });

println!("server running on localhost:6142");

tokio::run(server);}

Page 69: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

We usedFutures 0.1 to build stuff!

Page 70: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

The design had some problems

Page 71: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Futures 0.2

trait Future { type Item; type Error;

fn poll(&mut self, cx: task::Context) -> Poll<Self::Item, Self::Error>;}

No implicit context, no more need for thread local storage.

Page 72: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 73: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 74: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

// with callbackrequest('https://google.com/', (response) => { // handle response})

// with promiserequest('https://google.com/').then((response) => { // handle response});

// with async/awaitasync function handler() { let response = await request('https://google.com/') // handle response}

Async/await

Page 75: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Async/await lets you write code that feels synchronous, but is actually asynchronous

Page 76: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Async/await is more important in Rust than in other languages because Rust has no garbage collector

Page 77: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust example: synchronous

fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error>

let mut buf = [0; 1024];let mut cursor = 0;

while cursor < 1024 { cursor += socket.read(&mut buf[cursor..])?;}

Page 78: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust example: async with Futures

fn read<T: AsMut<[u8]>>(self, buf: T) -> impl Future<Item = (Self, T, usize), Error = (Self, T, io::Error)>

… the code is too big to fit on the slide

The main problem: the borrow checker doesn’t understand asynchronous code.

The constraints on the code when it’s created and when it executes are different.

Page 79: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Rust example: async with async/await

async { let mut buf = [0; 1024]; let mut cursor = 0;

while cursor < 1024 { cursor += socket.read(&mut buf[cursor..]).await?; };

buf}

async/await can teach the borrow checker about these constraints.

Page 80: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Not all futures can error

trait Future { type Item; type Error;

fn poll(&mut self, cx: task::Context) -> Poll<Self::Item, Self::Error>;}

Page 81: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

std::future

pub trait Future { type Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>;}

Pin is how async/await teaches the borrow checker.

If you need a future that errors, set Output to a Result<T, E>.

Page 82: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

… but one more thing...

Page 83: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

What syntax for async/await?

async is not an issue JavaScript and C# do:

await value;

But what about ? for error handling?

await value?;

await (value?);(await value)?;

Page 84: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

What syntax for async/await?

What about chains of await?

(await (await value)?);

Page 85: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

We argued and argued and argued and argued and argued and ar...

Page 86: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 87: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

What syntax for async/await?

async { let mut buf = [0; 1024]; let mut cursor = 0;

while cursor < 1024 { cursor += socket.read(&mut buf[cursor..]).await?; };

buf}

// no errorsfuture.await// with errorsfuture.await?

Page 88: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

… there’s actually even one last issue that’s popped up

Page 89: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

… this talk is already long enough

Page 90: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Additional Ergonomic improvements

use runtime::net::UdpSocket;

#[runtime::main]async fn main() -> std::io::Result<()> { let mut socket = UdpSocket::bind("127.0.0.1:8080")?; let mut buf = vec![0u8; 1024];

println!("Listening on {}", socket.local_addr()?);

loop { let (recv, peer) = socket.recv_from(&mut buf).await?; let sent = socket.send_to(&buf[..recv], &peer).await?; println!("Sent {} out of {} bytes to {}", sent, recv, peer); }}

Page 91: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

WebAssembly?

Page 92: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

WebAssembly?Promise

Future

Promise

Page 93: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Finally landing in Rust 1.37

Or maybe 1.38

Page 94: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Finally landing in Rust 1.37

Or maybe 1.38

Page 95: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 96: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Finally landing in Rust 1.38!!!!1

Page 97: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is
Page 98: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Lesson: a world-classI/O system implementation takes years

Page 99: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Lesson: different languages have different constraints

Page 100: Rust’s Journey to Async/Await - QCon New York · Rust’s Journey to Async/Await Steve Klabnik. Hi, I’m Steve! On the Rust team Work at Cloudflare Doing two workshops! What is

Thank you!

@steveklabnik