expression

Cancellation-safe receive in a selection

A receive that loses a selection does not consume a message. The receiver can be used again. The fixture uses explicit branch priority to choose the alternative before receiving the queued value. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): other 9 Common mistake: Assuming every asynchronous operation has the same cancellation-safety guarantees as recv.

Formal Syntax & Structure

rust syntaxrust
tokio::sync::mpsc::Receiver::recv()

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    let (tx, mut rx) = tokio::sync::mpsc::channel(1);    tx.send(9u32).await.unwrap();    tokio::select! {        biased;        _ = async {} => println!("other"),        _ = rx.recv() => unreachable!(),    }    println!("{}", rx.recv().await.unwrap());}
Rust Reference