Sub topic 1
What async fn and .await actually give you
Prerequisites
This chapter assumes you know Rust ownership and borrowing, traits and generics, and how to add a crate with Cargo. Familiarity with std::thread is helpful. Later chapters cover polling and the Tokio runtime in detail. Here you only need the basic model.
An async fn returns a value, not a result
Calling an async fn does not run its body. It returns a future, a value that describes work that has not happened yet. The compiler turns the function body into a state machine. It stores the local variables that must survive between pauses, plus a marker for where to resume.
use std::time::Duration;
use tokio::time::sleep;
async fn greet(name: &str) -> String {
sleep(Duration::from_millis(50)).await;
format!("hello, {name}")
}
#[tokio::main]
async fn main() {
let fut = greet("Ada");
println!("created the future");
let msg = fut.await;
println!("{msg}");
}The first line prints before any waiting happens, because greet("Ada") only builds the future. The body runs when fut.await drives it. Futures are lazy. If you drop one without awaiting it, nothing runs, and the compiler warns you about an unused future.
What .await does
.await is a suspension point. It does two things:
- It runs the inner future until it either finishes or cannot make progress.
- If it cannot make progress, the current future pauses and hands control back to whoever is driving it. That frees the thread for other work.
When the inner future finishes, execution continues on the next line with its output. .await is allowed only inside an async fn or async block.
What you do not get
Three misconceptions cause most beginner errors.
- No parallelism by itself. Awaiting two futures one after the other runs them one after the other. Two 100 ms sleeps awaited in sequence take 200 ms. Running them together needs
join!or spawned tasks, which later chapters cover. - No runtime in the standard library.
stddefines theFuturetrait and theasync/awaitsyntax, but nothing that actually runs futures. A runtime such as Tokio supplies that. - No protection from blocking. A call like
std::thread::sleepinside anasync fndoes not pause the future. It blocks the whole thread, and other work on that thread stalls.
Common errors
| Symptom | Cause |
|---|---|
| Warning: unused implementer of `Future` that must be used | The call returned a future that was never awaited |
| Error: `await` is only allowed inside `async` functions and blocks | `.await` used in a plain `fn`, including a non-async `main` |
| Two awaits take the sum of their times | They run sequentially, not concurrently |
Exercise
What is printed first by the program above, "created the future" or "hello, Ada", and why?
Show answer
"created the future" is printed first. Calling greet("Ada") only creates a lazy future and runs none of its body. The body, including the sleep and the format call, runs only when fut.await drives the future.
Exercise
A teammate writes let a = fetch(1).await; let b = fetch(2).await;, where each call waits 100 ms. How long does this take, and what does it show about .await?
Show answer
About 200 ms. Each .await finishes before the next line runs, so the waits add up. .await gives you suspension, not concurrency. To overlap the waits you must combine the futures, for example with tokio::join!.