Function tokio::sync::oneshot::channel

source ·
pub fn channel<T>() -> (Sender<T>, Receiver<T>)
Expand description

Creates a new one-shot channel for sending single values across asynchronous tasks.

The function returns separate “send” and “receive” handles. The Sender handle is used by the producer to send the value. The Receiver handle is used by the consumer to receive the value.

Each handle can be used on separate tasks.


use tokio::sync::oneshot;

async fn main() {
    let (tx, rx) = oneshot::channel();

    tokio::spawn(async move {
        if let Err(_) = tx.send(3) {
            println!("the receiver dropped");

    match rx.await {
        Ok(v) => println!("got = {:?}", v),
        Err(_) => println!("the sender dropped"),