The closures section of Rust documentation has this example:
fn call_with_ref<'a, F>(some_closure: F) -> i32
where F: Fn(&'a i32) -> i32
{
let value = 0;
some_closure(&value)
}
This doesn't compile because, as the docs put it:
When a function has an explicit lifetime parameter, that lifetime must be at least as long as the entire call to that function. The borrow checker will complain that value doesn't live long enough, because it is only in scope after its declaration inside the function body.
The error message being
error: `value` does not live long enough
--> <anon>:5:19
|
5 | some_closure(&value);
| ^^^^^ does not live long enough
...
8 | }
| - borrowed value only lives until here
I am having trouble understanding this. What does it mean that value does not live long enough? From what I understand value lives through the entire call of the function. So, where does the "not living long enough" come from?