In Rust, a sensor gateway calls an async fn that sends a reading but forgets the .await; what happens, and why?
answer
- futures are inert values
- the body runs only when polled
- dropped at the semicolon
- must_use on the Future trait
basics
~20 sNothing is sent. Calling an async fn only builds a future; its body runs when that future is polled, normally through .await. The unawaited future is dropped at the end of the statement, and rustc warns through unused_must_use.
solid answer
~40 sCalling `send_reading(7, 21.5)` evaluates the arguments and returns a value whose type implements `Future`; not one line of the body has run yet. Rust futures are lazy: work happens only inside `Future::poll`, which `.await` (or an executor at the top level) calls. Written as a bare statement, the future is a temporary that is dropped at the semicolon, so the reading is never sent and the next line runs as if the call were not there. rustc catches the common form because the `Future` trait is `#[must_use]`: the warning reads "unused implementer of `Future` that must be used" with the note "futures do nothing unless you `.await` or poll them". `let _ = send_reading(..)` discards it with no warning at all. The fix is `send_reading(7, 21.5).await?`.
code
rust · 23 linesuse futures::executor::block_on;
async fn send_reading(sensor: u32, celsius: f32) -> Result<(), String> {
println!("sending sensor {sensor}: {celsius} C");
Ok(())
}
async fn report() -> Result<(), String> {
send_reading(7, 21.5); // future built, then dropped: nothing sent
println!("report finished");
Ok(())
}
async fn report_fixed() -> Result<(), String> {
send_reading(7, 21.5).await?; // polled to completion
println!("report finished");
Ok(())
}
fn main() {
block_on(report()).unwrap();
block_on(report_fixed()).unwrap();
}go deeper
Say clearly that an async fn call only builds a future and that nothing runs until it is awaited or polled. Quote the must_use warning if you can.
Explain that the body runs inside Future::poll, that the forgotten future is a temporary dropped at the semicolon, and which forms rustc warns about and which it does not.
Show the review habits: deny unused_must_use in CI, distrust let _ = on async calls, and choose between awaiting, joining or spawning when the intent was concurrency.
Connect laziness to Rust's runtime-agnostic design: std defines the contract, the application picks the executor, and dropping a future is how work is cancelled.
## What calling an async fn gives you An **`async fn`** such as `async fn send_reading(sensor: u32, celsius: f32) -> Result<(), String>` does not return a `Result`. Calling it returns a value of an anonymous, compiler-generated type that implements **`Future<Output = Result<(), String>>`**. The call evaluates the arguments and moves them into that value, and that is all it does. How the compiler turns the body into that value is covered in the lesson on async functions and blocks; what matters here is that the body has not started. ## Futures are inert until something polls them The standard library states it plainly in the `Future` docs: futures alone are *inert*, and they must be *actively* polled for the computation to make progress. The only way into the body is the trait's one method, `Future::poll`. Three things call it: - **`.await`** inside another `async fn`, async block or async closure, which polls the inner future with the current task's context; - **an executor** at the top of the program, for example `futures::executor::block_on(report())`, which polls the outermost future until it returns `Poll::Ready`; - **hand-written code** that pins a future and calls `poll` itself, which tests and combinators do. Nothing else drives a future. Rust's standard library ships no runtime, so there is no background scheduler that quietly picks up a future you created and forgot. That is the difference from languages whose async functions start running the moment they are called: in Rust, creating a future and running it are two separate steps. ## What happens to the forgotten future Written as a bare statement, `send_reading(7, 21.5);` produces a **temporary** that is dropped at the end of the statement. Dropping a future that was never polled simply drops the captured arguments; the body never runs, the reading is never sent, and `report()` carries on to print `report finished`. No error is returned, because no `Result` was ever produced. Dropping a future is also how Rust cancels async work, which the lesson on cancellation covers in depth. ## What rustc tells you, and when it stays silent The `Future` trait carries `#[must_use = "futures do nothing unless you `.await` or poll them"]`, so the `unused_must_use` lint (warn by default, part of the `unused` group) fires on the bare statement. The forms behave differently: | Code inside an async fn | Body runs? | What rustc 1.99 reports | |---|---|---| | `send_reading(7, 21.5);` | no | warning: unused implementer of `Future` that must be used | | `let _ = send_reading(7, 21.5);` | no | nothing: `let _` discards the value deliberately | | `let _fut = send_reading(7, 21.5);` | no | nothing: the leading underscore silences unused_variables | | `send_reading(7, 21.5).await;` | yes | warning: unused `Result` that must be used | | `send_reading(7, 21.5).await?;` | yes | nothing; an `Err` now propagates | Writing `.await` inside a plain, non-async `fn` is a hard error instead: **E0728**, "`await` is only allowed inside `async` functions and blocks". ## Fixing it in a code review 1. Add `.await` where the result is needed, and handle the output: `send_reading(7, 21.5).await?`. 2. If the intent was to run the send alongside other work, hand the future to something that will poll it: a join of several futures, or an executor's spawn function. Both are covered in the lessons on concurrent futures and on spawning. 3. Treat the warning as an error in CI with `#![deny(unused_must_use)]` or `-D warnings`, so a forgotten `.await` cannot merge. 4. Be suspicious of `let _ = some_async_call(..)` in review: it silences the lint and still runs nothing. ## Why Rust made futures lazy - **No hidden cost.** Creating a future allocates nothing and schedules nothing; it is a plain value on the stack. - **The caller decides.** The same future can be awaited directly, joined with others, spawned, wrapped in a timeout, or dropped, and the choice is made where the future is used, not where it is created. - **Runtime-agnostic code.** Because std defines only the `Future` contract and no scheduler, a library future runs under whichever executor the application picked. - **Cancellation is a drop.** A future that is never polled again simply stops, at no extra cost. The price is exactly this bug: a call that looks like it does something but only builds a value. The `#[must_use]` warning is the compiler's main defence against it, which is why a warning-clean build matters in async Rust.
- Does `let _ = send_reading(7, 21.5);` run the function and ignore its result?No. `let _ =` drops the future immediately, so the body never runs; it only silences the `unused_must_use` warning. To run it and ignore the outcome you still need `.await`, for example `let _ = send_reading(7, 21.5).await;`, which discards the `Result` after the send has actually happened.
- Why can you not just write `send_reading(7, 21.5).await` inside a plain `fn main`?`.await` is only legal inside an async context, so rustc rejects it with E0728. A synchronous `main` needs an executor to poll the outermost future, such as `futures::executor::block_on`, or a runtime macro that generates that call for you.
- Is the un-awaited future ever polled later, when it is dropped?No. Dropping a future runs the destructors of whatever it holds, here the captured arguments, and nothing else. Its body never starts, so no partial send happens and no error surfaces.
Calling an async fn is like filling in an order slip: the slip records exactly what to cook, but nothing happens until someone hands it to the kitchen. Throw the slip away and no meal is ever made, and nobody tells you.
- Calling an async fn starts it in the background; .await only waits for the result.
- Forgetting .await is a compile error, so it cannot ship.
- let _ = fut; runs the future and throws away its output.
- A dropped future gets polled to completion on its way out.
- Rust's standard library has a default executor that runs stray futures.