I was playing with lifetimes and trying to restrict moving data to closures. I was checking if data was moved to the closure with let _ = {capture}
. It seamed that closures were always capturing my data no matter what until I tried to acrtually access my captures…
Code:
fn test_scope(data: &Vec<u8>) {
run_scope(|| {
let _ = data; // compiles
// let _wtf = data; // doesn't compile
// let _ = &data; // doesn't compile
// let _ = data.len(); // doesn't compile
});
}
fn run_scope(f: impl Fn() + 'static) { // yes, it must be 'static here
f()
}
I understand why bottom 3 cases don’t compile and I expect that.
What is a mistery to me is why does #1 compile… Closure is `static
and without move, it should not be allowed to capture anything that is not static. But my &Vec<u8>
is not static yet let _
seams to have access/capture to it.
Can please someone describe what is happening here?