January 2019
Intermediate to advanced
520 pages
14h 32m
English
To send a message from one thread to another, you are likely to use the mpsc module of the standard library. The mpsc module of the futures crate works in a similar way, but the Sender returns the Sink instance when you call the send method to send an item to the message stream. The Receiver implements the Stream trait, which means you have to use a reactor to poll the stream for new messages:
fn multiple() { let (tx_sink, rx_stream) = mpsc::channel::<u8>(8); let receiver = rx_stream.fold(0, |acc, value| { future::ok(acc + value) }).map(|x| { println!("Calculated: {}", x); }); let send_1 = tx_sink.clone().send(1); let send_2 = tx_sink.clone().send(2); let send_3 = tx_sink.clone().send(3); let execute_all = future::join_all(vec![ ...Read now
Unlock full access