When rustc decides whether an `async fn`'s future is `Send`, which values does it count as held across an `.await`?
answer
- arguments are always stored
- scope, not last use
- temporaries die at the semicolon
- drop() fails once the value was borrowed
- the awaited future counts too
basics
~20 sThe future holds every argument of the async fn, every local whose scope spans an .await even if never used again, temporaries alive at the await, and the future being awaited. One non-Send value among them makes it non-Send.
solid answer
~50 sAn `async fn` future is `Send` exactly when every value it may store is `Send`. It stores all of its **arguments**, captured at the call, so an `Rc` parameter makes the future non-`Send` whatever the body does. It stores every **local whose scope spans an `.await`**: rustc goes by scope, not last use, so an `Rc` created before the await and never touched again still counts, and shadowing the name does not end the old binding. An explicit `drop(tag)` removes it only if `tag` was never borrowed: once code has taken a reference to it (`format!("{tag}")`, `*tag`, `tag.clone()`), rustc 1.99.1 keeps its storage in the future until the scope ends. It stores **temporaries** only while they are alive, so one created and dropped in an earlier statement, or `let _ = Rc::new(..)`, does not count. It also stores the **future being awaited**, so awaiting a non-`Send` future makes the caller non-`Send`. The reliable fix is an inner block that ends the value's scope before the await.
code
rust · 16 linesuse std::rc::Rc;
async fn send_line(line: String) { let _ = line; }
fn require_send<T: Send>(_: T) {}
async fn explicit_drop(user: String) {
let tag = Rc::new(String::from("room-1"));
let line = format!("{tag}: {user}");
drop(tag); // moved out, but format! borrowed tag, so its storage stays live
send_line(line).await;
}
fn main() {
require_send(explicit_drop("ana".into()));
}go deeper
Remember that a future keeps arguments and in-scope locals at each await, so an Rc or a lock guard in scope there makes it non-Send.
Explain the four categories, arguments, locals by scope, live temporaries and the awaited future, and why an inner block works where shadowing, or drop() after a borrow, does not.
Diagnose from the note naming the binding and the await, restructure into blocks or sync helpers, and guard public async APIs with a require_send test so Send cannot silently disappear.
Treat the Send-ness of public async functions as part of the API contract, since it leaks through the opaque return type, and decide whether the team asserts it in tests or with a lint.
## The future is a struct you never wrote Calling an `async fn` returns an anonymous type that implements `Future`. The compiler builds it as a **state machine**: each `.await` is a point where `poll` may return `Poll::Pending`, and the type must store everything needed to resume from there. `Send` is an **auto trait**, so this type is `Send` only if every value it may store is `Send`. The practical question is which values rustc decides it may store; the compiler's notes call such a value "used across an await". ## What is counted 1. **Every argument.** The arguments move into the future when the function is called, before any line of the body runs, and they stay there. `async fn f(n: Rc<u32>)` returns a non-`Send` future even if the body copies `*n` out on its first line. 2. **Every local whose scope spans an `.await`.** rustc reasons about scopes here, not about the last use. `let tag = Rc::new(..)` followed by an await keeps `tag` in the future even if the code never mentions `tag` again. 3. **Temporaries alive at the `.await`.** A temporary lives to the end of its enclosing statement, or for a `match` or `if let` scrutinee, through the body, which is how a lock guard can hide in a one-line `match`. 4. **The future being awaited.** `x.await` stores `x` while it is pending, so awaiting a non-`Send` future makes the outer future non-`Send` as well. ## What is not counted - A local declared in an **inner block** that closes before the `.await`. - A **temporary** created and dropped within an earlier statement, for example an `Rc` built inside a `format!` argument list in a `let` that ends before the await. - A value bound to the **`_` pattern**: `let _ = Rc::new(5);` drops the value immediately, so nothing remains to store. - Values produced after the last `.await`, since no suspension follows them. - A `Send` value that merely came from a non-`Send` one, such as a `String` formatted from an `Rc<String>` inside a block: only the `String` survives to the await. ## The surprises worth knowing | code before the await | counted? | reason | |---|---|---| | `let tag = Rc::new(..);` never used again | yes | its scope spans the await | | `let tag = Rc::new(..); drop(tag);`, never borrowed | no | the value is moved out and nothing can still point at it | | `format!("{tag}")`, then `drop(tag);`, then await | yes, in rustc 1.99.1 | a borrowed local keeps its storage until its scope ends | | `let n = *rc; let rc = n;` (shadowing) | yes | shadowing hides the old binding, it does not end it | | `{ let tag = Rc::new(..); .. }` | no | the block ended the scope | | `let _ = Rc::new(5);` | no | `_` does not bind, the temporary is dropped at once | The `drop` rows surprise people most. The Async Book still says that dropping before the await "does not work today"; on rustc 1.99.1 that is true only for a local that was **borrowed** first. Once a reference to `tag` has been taken, the compiler cannot prove no reference survives, so it keeps the local's storage live until its scope ends and prints "await occurs here, with `tag` maybe used later" despite the `drop`. Real code almost always reads the value before dropping it, so treat `drop` as unreliable here; the compiler would rather reject a future it cannot prove is `Send` than accept an unsound one. ## How to use this when a build breaks - Read the note "future is not `Send` as this value is used across an await": it names the binding and the `.await`, and the `help` line names the non-`Send` type inside it. - Move the non-`Send` work into an **inner block** or a plain synchronous helper that returns `Send` data, such as a `String` built from an `Rc`. - Change **argument types** of async fns you intend to spawn: take `Arc<T>` instead of `Rc<T>`, owned data instead of borrowed. - To make a library's guarantee explicit, add a test such as `fn require_send<T: Send>(_: T) {}` called on the future, so a regression fails in your crate instead of in a caller's. That last point matters because an `async fn`'s returned type is opaque, yet its `Send`-ness leaks to callers. A change deep inside a body can silently remove `Send` from a public async function, which the material on API evolution treats as a compatibility hazard.
- Why is `async fn f(n: Rc<u32>)` never `Send`, even if the body copies the number out and never touches `n` again?Arguments move into the future at the call, before any of the body runs, and the parameter's scope is the whole body, so `n` is stored across every `.await` in it. Take an `Arc<u32>` or a plain `u32` instead, or convert to `Send` data before calling.
- How can a library catch the moment one of its public async functions stops being `Send`?Add a compile-time check in its own tests, such as `fn require_send<T: Send>(_: T) {}` called on the future, or enable clippy's `future_not_send` lint, which is in the nursery group and allowed by default. Either fails in the library's CI instead of in a downstream crate that spawns the future.
A future is a suitcase repacked at every layover, and an .await is the border crossing. Anything still in the case at the crossing is inspected, even items you never plan to unpack again; leaving an item on the bench before the crossing is what keeps it out, while an item you have already lent out is still listed on the manifest even after you throw it away (drop after a borrow).
- Only values used after the .await are stored in the future.
- Shadowing a variable ends the old binding's lifetime.
- drop(x) before the .await always removes x from the future.
- An argument the body never uses after the first line is not stored.
- Awaiting a non-Send future inside a Send one is fine.