Rust provides what it calls futures for implementing concurrency. Similar to:
Promise.std::future<T>.Alternatives:
A function that returns something that will be ready in the future.
async fn expensive() -> i32 {
0
}
// Same as:
fn expensive() -> impl Future<Output = i32> {
// Doesn't run until awaited.
async { 0 }
}
#[tokio::main]
async fn main() {
// Await the Future's completion. Yields control in the meantime.
expensive().await // must be inside an async function
}
Futures are lazy. They do nothing until awaited.
Useful for: