Excerpt from the Rust book: outside code blocks waiting for async code

Hello,

Sorry for making multiple posts on the topic I did not implement futures yet myself hence I have trouble understanding them well.
In an earlier post I asked whether a program can make progress while it is waiting for a future to be executed and I got a "depending on what happens inside trpl::run, yes" answer.

I just read this excerpt from the book: Applying Concurrency with Async - The Rust Programming Language

Note: Because all of this async code runs in an async block in a trpl::run call, everything within it can avoid blocking. However, the code outside it will block on the run function returning. That’s the whole point of the trpl::run function: it lets you choose where to block on some set of async code, and thus where to transition between sync and async code. In most async runtimes, run is actually named block_on for exactly this reason.

So this simply means: whenever I execute a future with a runtime like trpl, my code blocks until the future is complete. The code inside the async block passed to the runtime can make progress while waiting for the future to be available.
Correct?

That is what the async runtime function conventionally known as block_on() does. Runtimes may offer other ways of running futures which do not block the caller — usually called spawn(), but there can be other things too.

(I personally prefer the term “async executor” over “async runtime” for this topic, because it makes it clearer what its role is, whereas “runtime” covers a lot of very different things.)

The code inside the async block is not doing any of the waiting for the future; the runtime/executor is. The code inside the async block is the definition of when the future becomes ready; everything about the future’s behavior is defined by the code inside the async block.

1 Like

Thanks!