Rust joining thread cannot guarantee correct lifetime

34 Views Asked by At

I tried to create a simple example with threads:

let str = String::from("string");
let thread1 = thread::spawn(|| { //error: may outlive borrowed value `str`
    println!("{}", str);
});
thread1.join();

Since I call thread1.join() there is no way that the closure may outlive the borrowed value str. Is there a way to tell rust compiler about it somehow or the only way is to move ownership?

0

There are 0 best solutions below