use std::thread;
let nums1 = vec![1, 2, 3];
let handle = thread::spawn(move || {
println!("nums1 is {:?}", nums1);
});
//nums1.push(4); <-Error
handle.join().unwrap();
let mut nums2 = vec![1, 2, 3];
std::thread::scope(|s| {
s.spawn(|| {
println!("nums2 is {:?}", nums2);
});
});
nums2.push(4);
println!("nums2 is {:?}", nums2);
| Use the "move" keyword to indicate that the variable is passed to the secondary thread and will not be used in the main thread. std::thread::scope provides the necessary guarantee that any spawned threads will complete before returning, allowing data to be borrowed safely. |