跳到主要内容

channel

搜索

函数 channel 

Source
pub fn channel<T>() -> (Sender<T>, Receiver<T>)
展开描述

创建一个新的 one-shot channel,用于在异步任务之间发送单个值。

该函数返回单独的“send”和“receive”句柄。 Sender 句柄由生产者用于发送值。 Receiver 句柄由消费者用于接收值。

每个 handle 可以在不同的任务上使用。

§示例

use tokio::sync::oneshot;

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"),
}