pub fn channel<T>() -> (Sender<T>, Receiver<T>)
Available on crate feature
sync
only.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.
Examples
use tokio::sync::oneshot;
#[tokio::main]
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"),
}
}