std-method

Receiver::recv_timeout: bounded wait

Wait up to a requested interval for a message, distinguishing timeout from disconnection. A timeout does not cancel the producer. Scheduling can delay return; this fixture uses a zero wait and fixed channel states. Expected stdout (not executed): true 4 true Common mistake: Assuming timeout stops the worker or guarantees an exact response time.

Formal Syntax & Structure

rust syntaxrust
receiver.recv_timeout(Duration) -> Result<T, RecvTimeoutError>

Working Example

Example Snippetrust
fn main() {    use std::{sync::mpsc::{self, RecvTimeoutError}, time::Duration};    let (tx, rx) = mpsc::channel::<u8>();    println!("{}", matches!(rx.recv_timeout(Duration::ZERO), Err(RecvTimeoutError::Timeout)));    tx.send(4).unwrap();    println!("{}", rx.recv_timeout(Duration::ZERO).unwrap());    drop(tx);    println!("{}", matches!(rx.recv_timeout(Duration::ZERO), Err(RecvTimeoutError::Disconnected)));}
Rust Reference