expression

select!: race owned futures

Wait on alternative branches and evaluate the handler of a matching completed branch. Directly owned losing futures are dropped when the macro returns. Avoid assuming a deterministic winner when multiple branches are ready. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 8 Common mistake: Assuming all branches run to completion.

Formal Syntax & Structure

rust syntaxrust
tokio::select! { pattern = future => handler, ... }

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    let result = tokio::select! {        value = async { 8 } => value,        _ = std::future::pending::<()>() => 0,    };    println!("{result}");}
Rust Reference