4 ms·
I thought async and await were kind of logical builds on top of generators but this is saying rust has no generators. That’s how it works in javascript and as
by andrewstuart 3y ago
I thought async and await were kind of logical builds on top of generators but this is saying rust has no generators.
That’s how it works in javascript and as a commenter mentions in Python too, right?
- bobbylarrybobby 3y agoThey are logical builds on top of coroutines, as are generators. There is a generator initiative in the works, but it does seem odd that async exists and generators don’t given that the “pausable state machine” nature of them is so similar.
- potatochup 3y agoThat's true for implementations like python async, but rust operates a bit differently (mostly to avoid allocations across await points). The author of this article has some other very good posts about it.
- __s 3y agoThey're intertwined, but generators have yet to stabilize https://doc.rust-lang.org/beta/unstable-book/language-features/generators.html https://doc.rust-lang.org/beta/unstable-book/language-featur...
- Sprocklem 3y agoThey are in some languages, but Rust instead implemented them with a pollable Future trait. The async keyword, then, causes the block to desugar into a new type that implements Future. In order to emulate a coroutine, the generated poll function includes a state machine that tracks the await point where the function was suspended.
- kmeisthax 3y agoIt's also how it works in Rust. Stable Rust isn't allowed to use generators, but the code that desugars `async fn` is, because it's part of the compiler.