Consider the following Rust code:
use std::thread;
fn main() {
bar();
loop {}
}
fn bar() {
let var = b"foo";
thread::spawn(|| {
write(var);
});
}
fn write(d: &[u8]) {
println!("{:?}", d)
}
To my understanding, var
is on the stack of function bar
, which no longer exists after it returns.
Still, the new thread accesses it afterwards and successfully writes the data.
- Why does this work?
- Why doesn't the Rust compiler complain?