Async functions and lazy futures
Quick answer
An async function returns a future representing work that can finish later. Calling it creates the future; its body begins executing when the future is polled.
Separate creation from execution
async fn and async { ... } create future values. Inside an async context .await polls a future and may suspend until it is ready. Rust supplies the syntax and Future trait but not a general executor. These examples use immediate futures and a no-op waker to illustrate polling only; do not use this as a runtime for pending I/O.
async fn work() { println!("body"); }fn main() { let pending = work(); println!("created"); drop(pending);}Expected stdout (not executed):
createdDropping the unpolled future never enters its body.
use std::{future::Future, task::{Context, Poll, Waker}};async fn double(n: u32) -> u32 { n * 2 }fn main() { let mut f = Box::pin(async { double(7).await + 1 }); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(15)); println!("15");}Expected stdout (not executed):
15The immediate nested future finishes in the first poll.
Common mistake
Do not assume calling an async function starts a background task.
Try it yourself
Change double to add one and predict the outer future's output.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Async functions and lazy futures — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Calling an async function returns a future that is immediately dropped without polling. What happens to its body?
Async functions and lazy futures — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust async fn work() { println!("body"); } fn main() { let pending = work(); println!("created"); drop(pending); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Async functions and lazy futures — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::Future, task::{Context, Poll, Waker}}; async fn double(n: u32) -> u32 { n * 2 } fn main() { let mut f = Box::pin(async { double(7).await + 1 }); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(15)); println!("15"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.