November 2017
Intermediate to advanced
264 pages
5h 45m
English
An elegant code pattern is shown in the following code snippet, where the channel is created in the make_chan() function, which returns the receiving endpoint for the calling code:
// code from Chapter 9/code/make_channel.rs:
use std::sync::mpsc::channel;
use std::sync::mpsc::Receiver;
fn make_chan() -> Receiver<i32> { let (tx, rx) = channel(); tx.send(7).unwrap(); rx}
fn main() {
let rx = make_chan();
if let Some(msg) = rx.recv().ok() {
println!("received message {}", msg);
};
}
This prints out the following:
received message 7
Read now
Unlock full access