I am trying to understand why the following code is invalid. The error message is complaining that the temporary x
is dropped while still in use, but I don't understand how it would still be in use. When the function f
returns there should be no borrows of x
, so it should be safe to drop. What does it think the lifetime 'a
is?
fn foo<'a, F: Fn(&'a usize)>(f: F) {
let x = &mut 0_usize;
f(x);
}
Error:
error[E0716]: temporary value dropped while borrowed
--> src/arena.rs:43:18
|
1 | fn foo<'a, F: Fn(&'a usize)>(f: F) {
| -- lifetime `'a` defined here
2 | let x = &mut 0_usize;
| ^^^^^^^ creates a temporary which is freed while still in use
3 | f(x);
| ---- argument requires that borrow lasts for `'a`
4 | }
| - temporary value is freed at the end of this statement