std-method

Receiver::try_iter: drain available messages

Consume currently available messages without waiting for future ones. This is not a permanent closure signal or an atomic snapshot of all producer activity. Expected stdout (not executed): [1, 2] [3] Common mistake: Assuming the iterator is guaranteed to return after a fixed count while producers keep sending.

Formal Syntax & Structure

rust syntaxrust
receiver.try_iter() -> TryIter<'_, T>

Working Example

Example Snippetrust
fn main() {    use std::sync::mpsc;    let (tx, rx) = mpsc::channel();    tx.send(1).unwrap(); tx.send(2).unwrap();    let first: Vec<_> = rx.try_iter().collect();    tx.send(3).unwrap();    let second: Vec<_> = rx.try_iter().collect();    println!("{:?} {:?}", first, second);}
Rust Reference