expression

Handle abandoned oneshot replies

A dropped sender means the receiver cannot obtain a reply. A dropped receiver makes sending fail and returns the unsent value. Treat these transport failures separately from application-level errors. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): true 9 Common mistake: Unwrapping the receive result in production when a worker can stop before replying.

Formal Syntax & Structure

rust syntaxrust
rx.await -> Result<T, RecvError>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, rx) = tokio::sync::oneshot::channel::<u8>();    drop(tx);    println!("{}", rx.await.is_err());    let (tx, rx) = tokio::sync::oneshot::channel();    drop(rx);    println!("{}", tx.send(9).unwrap_err());}
Rust Reference