by klodolph · 1w ago · 0 comments
I assure you this is not pedantry or a nitpick. The classic case here is if you want to borrow a local variable inside a thread… you need to ensure that the thread lifetime is bounded from above by the lifetime of the borrowed variable. This is why we have weird workarounds like std::thread::scope. If we could just express an upper bound on lifetime, it wouldn’t be necessary.
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("{:?}", v); // borrows `v`
});
handle.join().unwrap();
}
If lifetimes could have upper bounds then this would work.