expression

Drain accepted work on shutdown

Close the receiving side to stop ordinary new sends, then drain buffered work. Outstanding reserved permits may still send before drain completes. This fixture has no outstanding permits. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true 10 Common mistake: Dropping the receiver immediately when the contract requires finishing accepted jobs.

Formal Syntax & Structure

rust syntaxrust
Receiver::close(); while let Some(item) = recv().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, mut rx) = tokio::sync::mpsc::channel(2);    tx.send(4u32).await.unwrap();    tx.send(6u32).await.unwrap();    rx.close();    println!("{}", tx.send(8).await.is_err());    drop(tx);    let mut total = 0;    while let Some(n) = rx.recv().await { total += n; }    println!("{total}");}
Rust Reference