Last active
August 16, 2020 09:41
-
-
Save justanotherdot/9a8cf84be4ba17326903f4eb5f4974d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let x = String::from("foo"); | |
// this does not work because it uses the special `'static` lifetime. | |
// which means _the entire program_. The main thread could finish and this | |
// thread could still be running! In that case the reference to x would be invalid. | |
// we can make this work by passing `move` in front of the closure, | |
// effectively copying `x` into the closure's environment. | |
// let normal = std::thread::spawn(|| dbg!(&x)); | |
// normal.join(); | |
// this works because it adopts the lifetime of `main`. | |
let scoped = crossbeam::thread::scope(|s| { | |
let child = s.spawn(|_| dbg!(&x)); | |
child.join() | |
}); | |
dbg!(&scoped); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment