Expand description
Returns aFuture instead of blocking the current thread.
Useasync in front offn,closure, or ablock to turn the marked code into aFuture.As such the code will not be run immediately, but will only be evaluated when the returnedfuture is.awaited.
We have written anasync book detailingasync/await and trade-offs compared to using threads.
§Control Flow
return statements and? operators withinasync blocks do not causea return from the parent function; rather, they cause theFuture returned by the block toreturn with that value.
For example, the following Rust function will return5, causingx to take the! type:
In contrast, the following asynchronous function assigns aFuture<Output = i32> tox, andonly returns5 whenx is.awaited:
Code using? behaves similarly - it causes theasync block to return aResult withoutaffecting the parent function.
Note that you cannot usebreak orcontinue from within anasync block to affect thecontrol flow of a loop in the parent function.
Control flow inasync blocks is documented further in theasync book.
§Editions
async is a keyword from the 2018 edition onwards.
It is available for use in stable Rust from version 1.39 onwards.