Skip to content
0:00 / 0:00

#futures#async/await#tokio

The What and How of Futures and async/await in Rust

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 7 people: 7 in engineering communities.
  • A classic. People kept recommending it from 2018 to 2023.
  • Presented by Jon Gjengset
  • Linked in the descriptiontokio-rs/doc-push

Advanced. Deep dive for people who know the field.

120K views7 years ago7 recommendations

Programming languagesSystems programmingRustConcurrency and parallelismNetworking and protocols

Jon Gjengset explains the inner workings of Rust's futures, pinning, and the async/await state machine.

Futures are Rust's way of expressing asynchronous computations, but even after reading the documentation, it can be hard to figure out how all the pieces of futures (and tokio) fit together. While this may not matter too much if you're just *using* futures, it becomes a stumbling block once you want to implement asynchronous primitives yourself. The highly anticipated async and await keywords that are coming down the pike promise to greatly reduce the friction of writing and using asynchronous code, but add even more to the mystery of how this stuff all works. NOTE: The YouTube transcoding process seems to have introduce audio lag on all the non-1080p versions. I'll report it to YT, but if it's bothering you, the 1080p version works fine :) In this stream, we tackle all of this head on. We go through what futures are, how they work, how they are used to represent things like asynchronous I/O (like networking), how they are executed, and how they are evolving as they are being adopted into the standard library. We also discuss async/await, and how they affect futures, and how Rust's proposed pinning types act as the glue to make async/await work. This is pretty dense material, but hopefully serves as a good and thorough overview of futures in Rust. If you want to know how it all works, this stream probably has you covered! To help you navigate, here are the major transition points: - Introduction - What are futures? - How are futures executed? - Super weird audio problems - All good again! (you didn't miss anything) - How do you integrate futures with I/O? - Driving futures with Tokio - Multi-threaded future execution - Stream and Sink - (brief aside about tokio-io-pool) - Putting futures in the standard library - Arbitrary self types - What is async/await? - Why is async/await hard? - Self-referential data-types - Pinning! - Q&A The tokio documentation push is being organized at github.com/tokio-rs/doc-push. You can vote for the next stream topic at jon.thesquareplanet.com/live-coding/ ! Live version with chat history here: youtu.be/fYE4qm6rbLE

Why it's here

7 people recommend this

What people say. Highly recommended as an exceptionally deep and clear explanation of Rust's async ecosystem, futures, and pinning.

Moments people linked to

u/idevthereforeiamin r/rust • Recommends

On “A Rust controller for Kubernetes”

Pinning is an advanced topic which requires quite a lot of prerequisite knowledge to be able to understand in a blog post. If you’re interested in learning more, check out this video. If you don’t know much about futures / async state machines then it’s probably worth watching this one first.

u/barskernin r/rust • Recommends

On “Trouble Understanding the Futures ecosystem”

I don't know if this is what you're looking for, but Jon just made a video which goes quite in depth about the whole ecosystem regarding futures and tokio. I learned a lot from it. link to this video Edit: To elaborate, the state of futures and tokio is currently being developed hence its perhaps not the best time to start learning. This might be some of the reason that it's frustrating.

u/barskernin r/rust • Recommends

On “std::future::Future stabilized (in 1.36)!”

The other answers are already great. Personally visualizations help me understand such concepts even better. /u/jonhoo made a great video on the topic which goes very in depth. link to this video

u/riasthebestgirlin r/rust • Recommends

On “Async Rust book - Why does the Future need to be boxed and pinned here?”

I recommend you watch these two videos: - The What, Why and How of Pinning - The What and How of Futures and async/await They're quite long but explain the concepts *extremely* well

u/JonhooRust for Rustaceansin r/rust • Recommends

On “Uncovered Intermediate Topics”

Something like link to this video and link to this video or did you have something else in mind?

u/kimsnjin r/rust • Recommends

On “How to learn about Rust's async story ? (and async in general)”

If you're interested in the underlying future and Tokio ecosystem, /u/jonhoo made a great stream making the tour of the ecosystem here: link to this video (note: it's 4h long). You can also check out the docs of https://tokio.rs/ If you want to play with some code, you could try some async web frameworks e.g. https://github.com/rustasync/tide (still very early development, focuses on exploring std::future usage and async/await) or https://github.com/actix/actif-web (that reached recently v1.0).

u/pkunk11in r/rust • Recommends

On “Anything like write your own tokio/async-std?”

link to this video

u/slikrick_in r/rust • Recommends

On “The What and How of Futures and async/await in Rust”

Please do not link reuploads, it's better to support the actual creator by viewing on his channel here: link to this video Especially as jonhoo is an active and helpful member of the community

u/riasthebestgirlin r/rust • Mentions

On “Announcing Rust 1.64.0”

…up. It may happen when the socket is ready, etc. At this point, future has finished and can now return Ready and control flows as usual. I hope that solves the misunderstanding of what's being awaited (and awaiting actually means). This is a fairly simplified explanation, I recommend you check out these videos that explains it in more detail: - link to this video - link to this video…

u/IAm_A_Complete_Idiotin r/rust • Mentions

On “How to understand Rust Pin and where I should use it?”

I realize it's a long video (at 4 hours), but sitting down and watching this should teach you about how async works and why it's implemented the way it is. It helped me a lot. He does go into Pin and Unpin and the like a 3 hours in. Edit: for the last bit - I believe everything *you* make with safe rust should be Unpin, but anything that uses self referential pointers internally shouldn't. In order to do that anyways you need to use unsafe code. link to this video

u/1vaderin r/rust • Mentions

On “Looking for resources to get better and learn the advanced stuff”

Have a look at John Gjengset. He does multi-hour long streams where he usually implements varyingly advanced stuff in Rust and shows the whole process. You can find recordings of all his streams on YouTube. They are usually quite long (4-6 hours) but you can just split them up yourself if you don't have that much time and watch them over multiple days or weeks or just watch a bit here and there if you find something interesting which will still help a lot. See also his video Why are my videos so damn long? In some streams, he also just goes into details about some advanced topics. For example, if you want to learn more about async and futures, have a look at [The What and How of Futures…

u/cfsamsonin r/rust • Mentions

On “Async Rust Primer?”

Yeah, Futures/async is quite the rabbit hole though... What I will recommend is this stream from Jon Gjengset: link to this video It's long, but does give a good understanding on how all the pieces fit together and explains how the OS does async itself, how Mio handles the lowest level interfacing I/O operations with the OS, how Futures is a structure with the necessary pieces to enable running async code in Rust but does nothing themselves, and how an executor (like Tokio) is needed to actually schedule execution and drive the whole process to completion. P.S. Disclaimer I'm still learning myself so if there's any incorrectness in the above section, please leave a comment. My guess regarding the wa…

u/lanklaasin r/rust • Mentions

On “With async/await, I don't get what runs the await task/future since there is no runtime or executor by default in Rust?”

This video really helped me get it: link to this video

u/maboesanmanin r/rust • Mentions

On “What is tokio exactly?”

If you want something REALLY in depth, this video is maybe the best explanation I’ve seen Edit: it’s 4 hours long btw

u/kruvasyanin r/rust • Mentions

On “what are some good resources to learn about Futures in depth?”

Jon Gjengset has a great YouTube video about Futures: [video Also you can find on his channel how tokio crate works: [video

u/Limp-Sherbetin r/rust • Mentions

On “Is my code garbage?(Struggled 8 hours in one sitting with trial and error)”

I would suggest taking a look into tokio, it can be pretty hard to grasp sometimes but I think it would great for your project You can also check out Jon Gjengset, he has pretty informative videos on a lot of Rust related concepts Before using tokio, you should get comfortable with futures, that helped me a lot tokio crate futures