Skip to lesson content
navan✳THE BUILD LABRUST
0/53 complete
BUILD LAB / RUST/ ASYNC & SYSTEM BOUNDARIES
BUILD NOTE 45 / 53

A future is work waiting to be driven.

Distinguish constructing asynchronous work from executing it.

BY NAVAN SAMI◷ 9 MIN READADVANCEDRUST 2024

01 Understand the contract

An async function returns a future: a state machine representing work that can make progress when polled. Calling it alone does not run the body to completion. await suspends the current future while another future is pending; it does not inherently spawn a thread. An executor such as Tokio polls ready tasks, while reactor infrastructure wakes work when I/O becomes ready. Use the cookbook’s Tokio/Axum projects to run async I/O. This standard-library example deliberately creates and drops a future to make laziness visible.

02 Make it concrete

This is a complete standard-library example. Use Rust stable with the 2024 edition. Run it locally using the Rust workbench.

lessons/async-futures/src/main.rs
async fn compute() -> u32 {
    println!("body ran");
    42
}
fn main() {
    let work = compute();
    println!("future created");
    drop(work);
    println!("future dropped without polling");
}
↳ EXPECTED OUTPUT
future created
future dropped without polling
  1. 1

    Calling compute constructs a future.

  2. 2

    No executor polls the future here, so its body does not print.

  3. 3

    The server cookbook uses #[tokio::main] to supply a runtime.

From the extracted rust-workbench folder
cargo run -p lesson_async_futures

03 Mind the boundary

04 Your next build

A SMALL MISSION

Create an async function returning a String, then show that constructing its future does not execute its body.

Reveal one possible solution +
solutions/async-futures/src/main.rs
async fn message() -> String {
    "ready".to_owned()
}
fn main() {
    let future = message();
    drop(future);
}

05 Check your reasoning

QUICK SIGNAL CHECK

Does calling an async fn by itself run its body to completion?

ONE MORE CONTRACT YOU CAN EXPLAIN.

Understand it.
Then make it yours.

Run the example, try your own input, and explain the boundary. Log the lesson when the idea makes sense.

Built with curiosity. By Navan Sami. ↗Rust · Make the contract clear. Then build.