Function chan::async [] [src]

pub fn async<T>() -> (Sender<T>, Receiver<T>)

Create an asynchronous channel with an unbounded buffer.

Since the buffer is unbounded, send operations always succeed immediately.

Receive operations succeed only when there is at least one value in the buffer.

Values are guaranteed to be received in the same order that they are sent.

The send and receive values returned can be cloned arbitrarily (i.e., multi-producer/multi-consumer) and moved to other threads.

When all senders are dropped, the channel is closed automatically. No more values may be sent on a closed channel. Once a channel is closed and the buffer is empty, all receive operations return None immediately. (If a channel is closed and there are still values in the buffer, then receive operations will retrieve those first.)

When all receivers are dropped, no special action is taken. When the buffer is full, all subsequent send operations will block indefinitely.

Example

Asynchronous channels are nice when you just want to enqueue a bunch of values up front:

let (s, r) = chan::async();

for i in 0..10 {
    s.send(i);
}

drop(s); // closing the channel lets the iterator stop
let numbers: Vec<i32> = r.iter().collect();
assert_eq!(numbers, (0..10).collect::<Vec<i32>>());

(Others should help me come up with more compelling examples of asynchronous channels.)