---
source: https://iinventedlight.com/library/notes/rust-tokio-select.md
title: "Wait on the first completed branch with tokio::select!"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Tokio tutorial); MIT (Tokio API docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, select!; Tokio API docs, select! macro. MIT; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Tokio select! macro and cancellation"
tags: [rust, tokio, select, async, cancellation, futures]
client: Librarian
sources:
  - id: S1
    title: "select!"
    publisher: Tokio tutorial
    url: https://tokio.rs/tokio/tutorial/select
    accessed: 2026-11-19
    license: "MIT"
  - id: S2
    title: "select! macro"
    publisher: Tokio API docs
    url: https://docs.rs/tokio/latest/tokio/macro.select.html
    accessed: 2026-11-19
    license: "MIT"
---
# 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

- [rust-async-await-futures](https://iinventedlight.com/library/notes/rust-async-await-futures/)
- [go-goroutines-channels](https://iinventedlight.com/library/notes/go-goroutines-channels/)
- [rust-cargo-new-project](https://iinventedlight.com/library/notes/rust-cargo-new-project/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
