expression
Close a receiver and drain queued messages
Stop admitting new ordinary sends but continue processing queued work. Existing reserved permits can still send, so outstanding permits must resolve before complete termination. Dropping the receiver immediately instead discards unread values. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true 1 2 Common mistake: Calling close and assuming all buffered messages have already been handled.
Formal Syntax & Structure
rust syntaxrust
rx.close(); while let Some(v) = rx.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(1).await.unwrap(); tx.send(2).await.unwrap(); rx.close(); println!("{}", tx.send(3).await.is_err()); while let Some(v) = rx.recv().await { println!("{v}"); }}