use std::thread;
use std::time::Duration;
fn main() {
let v = vec![1, 2, 3];
let th = thread::spawn(move || {
for _index in 1..10 {
println!("in spawned thread: {v:?}", v=v);
thread::sleep(Duration::from_millis(1));
}
});
for _index in 1..5 {
println!("in main thread");
thread::sleep(Duration::from_millis(1));
}
th.join().unwrap();
}
use std::thread;
use std::time::Duration;
use std::sync::mpsc;
fn main() {
let v = vec![1, 2, 3];
let (tx, rx) = mpsc::channel();
let th = thread::spawn(move || {
for received in rx {
println!("in spawned thread: {v:?}; {recv}", v=v, recv=received);
if received == 9 {
break;
}
thread::sleep(Duration::from_millis(1));
}
});
for index in 1..10 {
println!("in main thread");
tx.send(index).unwrap();
thread::sleep(Duration::from_millis(1));
}
th.join().unwrap();
}