Skip to content

Commit 6ee3bc3

Browse files
committed
Add "async-await hits beta" blog post
1 parent 2a670dc commit 6ee3bc3

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
layout: post
3+
title: "Async-await hits beta!"
4+
author: Niko Matsakis
5+
---
6+
7+
Big news! As of this writing, **syntactic support for async-await is
8+
available in the Rust beta channel!** It will be available in the 1.39
9+
release, which is expected to be released on **November 7th, 2019**.
10+
Once async-await hits stable, that will mark the culmination of a
11+
**multi-year effort to enable efficient and ergonomic asynchronous I/O
12+
in Rust**. It will not, however, mark the end of the road: there is
13+
still more work to do, both in terms of polish (some of the error
14+
messages we get today are, um, [not great]) and in terms of feature
15+
set ([async fn in traits], anyone?).
16+
17+
[not great]: https://github.com/rust-lang/rust/issues/64130
18+
[async fn in traits]: https://github.com/dtolnay/async-trait
19+
20+
(If you're not familiar with what async-await is, don't despair!
21+
There's a primer and other details later on in this post!)
22+
23+
### Async-await support in the ecosystem growing rapidly
24+
25+
But async-await has never been the entire story. To make good use of
26+
async-await, you also need strong libraries and a vibrant ecosystem.
27+
**Fortunately, you've got a lot of good choices, and they keep getting
28+
better:**
29+
30+
- the async runtime [tokio], for example, recently announced an [alpha
31+
release][] that supports async-await;
32+
- the [recently announced][] [async-std][] library was built from the
33+
start on the new async-await syntax;
34+
- using [wasm-bindgen-futures], you can even bridge Rust Futures with
35+
[JavaScript promises];
36+
- finally, in addition to the core runtimes just mentioned,
37+
async-await support is starting to become available in higher-level
38+
[web frameworks][wf] as well.
39+
40+
[wasm-bindgen-futures]: https://docs.rs/crate/wasm-bindgen-futures/0.2.16
41+
[tokio]: https://actix.rs/
42+
[actix]: https://actix.rs/
43+
[alpha release]: https://tokio.rs/blog/2019-08-alphas/
44+
[adding support]: https://github.com/actix/actix-web/issues/955#issuecomment-523266936
45+
[async-std]: https://async.rs/
46+
[recently announced]: https://async.rs/blog/announcing-async-std/
47+
[wf]: https://www.arewewebyet.org/topics/frameworks/
48+
[JavaScript promises]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
49+
50+
### Restructuring Async I/O in the Rust org
51+
52+
Now that async-await is stable, we are taking the opportunity to make
53+
some changes to our Rust team structure. The current structure
54+
includes two working groups: the [Async Foundations WG], focused on
55+
building up core language support, and the [Async Ecosystem WG],
56+
focused on supporting the ecosystem develop.
57+
58+
**In light of all the activity going on in the ecosystem, however,
59+
there it not as much need for the [Async Ecosystem WG], and as such
60+
we've decided to spin it down.** We'll be deprecating the [rustasync
61+
github org]. The [areweasyncyet.rs] and [arewewebyet.org] websites
62+
will move to the main [rust-lang org], but the fate of the other
63+
projects will be decided by the people who built them. A few will
64+
likely be deprecated, and the remainder will be moving out to be
65+
maintained independently.
66+
67+
[areweasyncyet.rs]: https://areweasyncyet.rs/
68+
[arewewebyet.org]: https://www.arewewebyet.org/
69+
[rustasync github org]: https://github.com/rustasync/
70+
[rust-lang org]: https://github.com/rust-lang/
71+
[Async Foundations WG]: https://rust-lang.github.io/compiler-team/working-groups/async-await/
72+
[Async Ecosystem WG]: https://github.com/rustasync/team
73+
[Async Book]: https://github.com/rust-lang/async-book
74+
75+
**The [Async Foundations WG], meanwhile, will continue, but with a
76+
shift in focus.** Now that async-await is en route to stabilization,
77+
the focus will be on polish, such as improving diagnostics, fixing
78+
smaller bugs, and improving documentation such as the [Async
79+
Book]. Once progress is made on that, we'll be considering what
80+
features to implement next.
81+
82+
### Async await: a quick primer
83+
84+
So, what is async await? Async-await is a way to write functions that
85+
can "pause", return control to the runtime, and then pick up from
86+
where they left off. Typically those pauses are to wait for I/O, but
87+
there can be any number of uses.
88+
89+
You may be familiar with the async-await from other languages, such as
90+
JavaScript or C#. Rust's version of the feature is similar, but with a
91+
few key differences.
92+
93+
To use async-await, you start by writing `async fn` instead of `fn`:
94+
95+
```rust
96+
async fn first_function() -> u32 { .. }
97+
```
98+
99+
Unlike a regular function, calling an `async fn` doesn't do anything
100+
to start -- instead, it returns a `Future`. This is a suspended
101+
computation that is waiting to be executed. To actually *execute*
102+
the future, you have to use the `.await` operator:
103+
104+
```rust
105+
async fn another_function() {
106+
// Create the future:
107+
let future = first_function();
108+
109+
// Await the future, which will execute it (and suspend
110+
// this function if we encounter a need to wait for I/O):
111+
let result: u32 = future.await;
112+
...
113+
}
114+
```
115+
116+
This example shows the first difference between Rust and other
117+
languages: we write `future.await` instead of `await future`. This
118+
syntax integrates better with Rust's `?` operator for propagating
119+
errors (which, after all, are very common in I/O). One can simply
120+
write `future.await?` to await the result of a future and propagate
121+
errors. It also has the advantage of making method chaining painless.
122+
123+
### Zero-cost futures
124+
125+
The other difference between Rust futures and futures in other
126+
languages is that they are based on a "poll" model, which makes them
127+
**zero cost**. In other languages, invoking an async function
128+
immediately creates a future and schedules it for execution: awaiting
129+
the future isn't really necessary for it to execute. But this implies
130+
some overhead for each future that is created.
131+
132+
In contrast, in Rust, calling an async function does not do any
133+
scheduling in and of itself, which means that we can compose a complex
134+
nest of futures without incurring a "per-future cost". As an end-user,
135+
though, the main thing you'll notice is that **futures feel "lazy"**:
136+
they don't do anything until you await them.
137+
138+
If you'd like a closer look at how futures work under the hood,
139+
[withoutboats] gave a [great introduction in this talk][video] from
140+
[Rust LATAM 2019].
141+
142+
[video]: https://www.youtube.com/watch?v=skos4B5x7qE
143+
[Rust LATAM 2019]: https://rustlatam.org/
144+
[withoutboats]: https://rustlatam.org/
145+
146+
### Summary
147+
148+
In summary, if you've an interest in using Async I/O in Rust, this is
149+
a very exciting time! With async-await syntax hitting stable in
150+
November, it's going to be easier than ever to write futures (in
151+
particular, if you tried using the combinator-based futures in the
152+
past, you'll find [async-await integrates much better with Rust's
153+
borrowing system][bc]). Moreover, there are a now a number of great
154+
runtimes and other libraries available in the ecosystem to work with.
155+
So get out there and build stuff!
156+
157+
(Oh, yeah, and please file bugs when you hit confusing or surprising
158+
problems, so we can improve the user experience!)
159+
160+
[bc]: http://aturon.github.io/tech/2018/04/24/async-borrowing/

0 commit comments

Comments
 (0)