std-method

Receiver::try_recv: check without waiting

Return a pending message or report Empty if connected but no data is available. Disconnected indicates there are no senders and nothing left to receive. An empty observation is not permanent. Expected stdout (not executed): true 9 true Common mistake: Treating Empty as permanent completion.

Formal Syntax & Structure

rust syntaxrust
receiver.try_recv() -> Result<T, TryRecvError>

Working Example

Example Snippetrust
fn main() {    use std::sync::mpsc::{self, TryRecvError};    let (tx, rx) = mpsc::channel::<u8>();    println!("{}", matches!(rx.try_recv(), Err(TryRecvError::Empty)));    tx.send(9).unwrap();    println!("{}", rx.try_recv().unwrap());    drop(tx);    println!("{}", matches!(rx.try_recv(), Err(TryRecvError::Disconnected)));}
Rust Reference