async
/.await
There are only two ways to use async
.
async
functionsasync
blocks
Both means return a value that implements the Future
trait. The following functions return the same type:
#![allow(unused)] fn main() { async fn get5() -> u8 { 5 } fn get5() -> impl Future<Output = u8> { async { 5 } } }
async
Lifetimes
Unlike regular functions, async
functions whose parameters are references or non-'static return a Future
which is bounded by the lifetime of the arguments. Meaning, the future returned from an async fn
must be .await
while its non-'static arguments are still valid.
async move
async move
works just like move
blocks used with closures.
.await
ing on a Multithreaded Executor
Future
s can move freely between threads, so any value used in async
stuff must be of a type that is also able to travel between threads (i.e. the type must implement Send
).