Wait on the first completed branch with tokio::select!
Answer: The tokio::select! macro allows waiting on multiple async computations and returns when the first one completes. The remaining branches are cancelled. The select! macro must be used inside of async functions, closures, and blocks.
Syntax
select! can handle more than two branches, with a current limit of 64 branches. The API docs describe each branch as <pattern> = <async expression> (, if <precondition>)? => <handler>,. It can also have one optional else branch, evaluated when none of the other branches match their patterns. The macro aggregates all async expressions and runs them concurrently on the current task. When the first expression completes and its value matches the pattern, select! returns the result of evaluating that branch's handler.
Cancellation
With asynchronous Rust, cancellation is performed by dropping a future. When select! returns, the futures on the branches that did not complete are dropped. That is cancelling the remaining branches.
| pattern | handler | |---|---| | Pattern | Must match the completed expression's value | | Async expression | Runs concurrently on the current task | | Optional if precondition | A precondition on the branch | | Handler | Evaluated for the completed branch | | Optional else | Runs if none of the other branches match | | 64 branches | The current limit in the tutorial |
The macro pattern includes an optional biased;.
See also
Claims
- id: C1 claim: select! waits on multiple async computations and returns when one completes. source: S1 evidence: "allows waiting on multiple async computations and returns when"
- id: C3 claim: The current limit is 64 branches. source: S1 evidence: "The current limit is 64 branches"
- id: C4 claim: When the first branch completes, the remaining branches are cancelled. source: S2 evidence: "cancelling the remaining branches"
Sources
- [S1] select!. Tokio tutorial. https://tokio.rs/tokio/tutorial/select . License: MIT. Accessed 2026-11-19.
- [S2] select! macro. Tokio API docs. https://docs.rs/tokio/latest/tokio/macro.select.html . License: MIT. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, select!; Tokio API docs, select! macro. MIT; MIT. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, select!; Tokio API docs, select! macro. MIT; MIT.
Cite this
iinventedlight. 2026. "Wait on the first completed branch with tokio::select!" iinventedlight Library, iil:note:2pk0c4fe, version of 2026-11-19. https://iinventedlight.com/library/notes/rust-tokio-select/. License: CC-BY-4.0.
@misc{iil_note_2pk0c4fe,
author = {{iinventedlight}},
title = {{Wait on the first completed branch with tokio::select!}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/rust-tokio-select/},
note = {Permanent ID iil:note:2pk0c4fe. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-tokio-select.table.json (table; CC-BY-4.0)
- rust-tokio-select.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-tokio-select.md.