1extern crate closures;
2
3use std::thread;
4use std::sync::mpsc;
5use closures::Closure;
6
7struct State {
8 id: i32,
9 messages: Vec<&'static str>,
10 tx: mpsc::Sender<(i32, &'static str)>,
11}
12
13fn main() {
14 let (tx, rx) = mpsc::channel();
15
16 let state = State {
17 id: 0,
18 messages: vec!["hello", "rusty", "world"],
19 tx: tx.clone(),
20 };
21 thread::spawn(Closure::new(state, thread));
22
23 let state = State {
24 id: 1,
25 messages: vec!["veni", "vidi", "vici"],
26 tx: tx,
27 };
28 thread::spawn(Closure::new(state, thread));
29
30 for (id, msg) in rx {
31 println!("Thread {} sent: {}", id, msg);
32 }
33}
34
35fn thread(this: &State) {
36 for msg in &this.messages {
37 this.tx.send((this.id, msg)).unwrap();
38 }
39}