async fn work() {
println!("work ran");
}
#[tokio::main]
async fn main() {
let f = work();
println!("created future");
f.await;
}What does this program print?
- a
work ranthencreated future, the body starting right at the call - b
created future, thenwork ran✓ - cOnly
created future; the future is dropped unpolled - dIt does not compile; a future must always be awaited where it is created
Explanation:Calling an
async fn builds a future and does nothing else. The body runs only when the future is polled, which happens at f.await. So the println! in main executes first, and work ran appears afterwards. Measured on Rust 1.98 / tokio 1.x: created future precedes work ran.