Rust 异步通信原语

深入理解 Rust 的异步通信机制

async-channel

异步、无界的多生产者多消费者通道,用于异步任务间的消息传递。

let (tx, rx) = unbounded();
tx.send(42).await?;
let value = rx.recv().await?;
中级 异步通信

async-oneshot

异步一次性通道,用于在异步任务之间进行一次性的消息传递。

let (tx, rx) = oneshot::channel::<String>();
tx.send("Hello".to_string()).unwrap();
let message = rx.await.unwrap();
中级 异步通信

Kanal

高性能、零成本的异步通道实现,专为Rust的异步编程设计。

let (tx, rx) = bounded::(10);
tx.send(42).await.unwrap();
中级 异步通信

Oneshot Channel

一次性使用的异步通信通道,用于在两个任务之间传递单个值。

let (tx, rx) = oneshot::channel();
tx.send("hello").unwrap();
let received = rx.await.unwrap();
中级 异步通信

Async-Oneshot Channel

无需运行时的异步一次性通道,专为无运行时环境下的异步通信设计。

let (sender, receiver) = oneshot();
sender.send(value).ok();
let result = receiver.await?;
高级 异步通信

Async Priority Channel

异步优先级通道,用于在异步环境中进行优先级消息传递。

let (sender, receiver) = PriorityChannel::new();
sender.send(2, "中等优先级").await.unwrap();
let (priority, msg) = receiver.recv().await.unwrap();
高级 异步通信