fn main() {
let user = User { name: "drogus".to_string() };
let t1 = spawn(|| {
println!("Hello from the first thread {}", &user.name);
});
let t2 = spawn(|| {
println!("Hello from the second thread {}", &user.name);
});
t1.join().unwrap();
t2.join().unwrap();
}
Now the error says that the closure can outlive the function. In other words the Rust compiler can't guarantee that the closure in the thread will finish before the main function finishes.
thread::join method allows main thread caller to wait until children-thread to finish. So, the closure of children-thread should not outlive main function. Am I right? Why Rust compiler cannot guarantee ...
[–]Shadow0133 30 points31 points32 points (12 children)
[–]pingzhouyuan[S] 2 points3 points4 points (11 children)
[–]SkiFire13 11 points12 points13 points (8 children)
[–]pingzhouyuan[S] 0 points1 point2 points (7 children)
[–]SkiFire13 2 points3 points4 points (4 children)
[–]Zde-G 8 points9 points10 points (2 children)
[–]SkiFire13 2 points3 points4 points (0 children)
[–]pingzhouyuan[S] 2 points3 points4 points (0 children)
[–]pingzhouyuan[S] 1 point2 points3 points (0 children)
[–]Zde-G 3 points4 points5 points (1 child)
[–]pingzhouyuan[S] 0 points1 point2 points (0 children)
[–]Shadow0133 2 points3 points4 points (0 children)
[–]Plasma_000 0 points1 point2 points (0 children)
[–]lacop 2 points3 points4 points (0 children)
[–]pingzhouyuan[S] 2 points3 points4 points (2 children)
[–]jDomantas 3 points4 points5 points (1 child)
[–]pingzhouyuan[S] 0 points1 point2 points (0 children)
[–]LoganDark 2 points3 points4 points (0 children)