[][src]Function async_std::sync::channel

pub fn channel<T>(cap: usize) -> (Sender<T>, Receiver<T>)
This is supported on unstable only.

Creates a bounded multi-producer multi-consumer channel.

This channel has a buffer that can hold at most cap messages at a time.

Senders and receivers can be cloned. When all senders associated with a channel get dropped, it becomes closed. Receive operations on a closed and empty channel return None instead of trying to await a message.


If cap is zero, this function will panic.


use std::time::Duration;

use async_std::sync::channel;
use async_std::task;

let (s, r) = channel(1);

// This call returns immediately because there is enough space in the channel.

task::spawn(async move {
    // This call will have to wait because the channel is full.
    // It will be able to complete only after the first message is received.

assert_eq!(r.recv().await?, 1);
assert_eq!(r.recv().await?, 2);