3 ms·
If you don't have the value type then you're constrained by the parent mut lifetime here so it's not just as simple as a conversion that you can then pass aroun
by danieldk 9y ago
If you don't have the value type then you're constrained by the parent mut lifetime here so it's not just as simple as a conversion that you can then pass around.
You cannot move any closure to spawn that captures a non-static reference, since there is no guarantee that the value will live during the lifetime of the thread. (The closure is required to be Send + 'static.) E.g., you can't do (moving to a non-Copy type :)):
let foo = String::new();
let t = thread::spawn(|| {
println!("foo: {}", &foo);
});
You could move the captures, including foo, but then foo cannot be used in its original scope anymore:
let foo = String::new();
let t = thread::spawn(move || {
println!("foo: {}", &foo);
});
// This will give an error, because foo is moved.
println!("{}", foo);
Note that only moving values works, this does not compile (for the same reasons as the first example):
let foo = String::new();
let bar = &foo;
let t = thread::spawn(move || {
println!("bar: {}", &bar);
});