跳到主要内容

channel

搜索

函数 channel 

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

创建一个新的一次性 channel,用于跨异步任务发送单个值。

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

每个句柄可以用于不同的任务。

§示例

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