expression

One response using a oneshot channel

Transfer one result from a producer to a waiting consumer. Sending consumes the handle and does not require await. Use this for a single reply, not a stream of progress updates. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 42 Common mistake: Trying to reuse the consumed oneshot sender for a second result.

Formal Syntax & Structure

rust syntaxrust
tokio::sync::oneshot::channel::<T>(); tx.send(value); rx.await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, rx) = tokio::sync::oneshot::channel();    let worker = tokio::spawn(async move { tx.send(6 * 7).unwrap(); });    println!("{}", rx.await.unwrap());    worker.await.unwrap();}
Rust Reference