concept channel in category rust

appears as: Chnels, chnel, channel
Rust in Action

This is an excerpt from Manning's book Rust in Action.

The lock approach, while workable, is cumbersome. Channels offer a simpler alternative.

Channels have two “ends”, a sending end and a receiving end. Programmers don’t get access to what is happening inside the channel. But, placing data at the sending end, they’ll appear at the receiving end at some stage in the future. They can be used as a task queue because multiple items can be sent, even if a receiver is not ready to receive any messages.

Channels are fairly abstract. They hide their internal structure, preferring to delegate access to two helper objects. One can send(), the other can recv() (receive). Importantly, we don’t get access to how channels transmit any information sent through the channel.

Listing 10. 25. Creating a channel that receives i32 messages from a worker thread.
#[macro_use] #1
extern crate crossbeam;  #2

use std::thread;
use crossbeam::channel::{unbounded};  #3

fn main() {
    let (tx, rx) = unbounded();  #4
    thread::spawn(move || {
        tx.send(42);
    });
    select!{  #5
       recv(rx) -> msg => println!("{:?}", msg),  #6
    }
}