std-type

TryRecvError: empty versus closed

Handle temporary lack of data separately from end of stream. When a producer may continue sending, a nonblocking check cannot predict the next message. Expected stdout (not executed): wait done Common mistake: Ending a consumer because it observed Empty once.

Formal Syntax & Structure

rust syntaxrust
TryRecvError::Empty; TryRecvError::Disconnected

Working Example

Example Snippetrust
fn main() {    use std::sync::mpsc::{self, TryRecvError};    let (tx, rx) = mpsc::channel::<()>();    match rx.try_recv() { Err(TryRecvError::Empty) => println!("wait"), _ => unreachable!() }    drop(tx);    match rx.try_recv() { Err(TryRecvError::Disconnected) => println!("done"), _ => unreachable!() }}
Rust Reference