Message Passing with Channels
Message Passing with Channels
“Do not communicate by sharing memory; instead, share memory by communicating.”
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let msg = String::from("order_payload");
tx.send(msg).unwrap(); // Ownership moves into channel!
});
let received = rx.recv().unwrap();
println!("Received through channel: {}", received);
}