Here is trivial reproduction (nightly rustc as of 2-feb-23):
fn main() {
let closure = |_v| {};
// this one works fine
// let closure = |_v: &_| {};
{
let x = 1;
closure(&x);
}
{
let y = 1;
closure(&y);
}
}
The error is:
6 | let x = 1;
| - binding `x` declared here
7 | closure(&x);
| ^^ borrowed value does not live long enough
8 | }
| - `x` dropped here while still borrowed
...
11 | closure(&y);
| ------- borrow later used here
Which doesn't make sense, as variable x
is not captured by the closure
, but is just an argument passed by reference.
Providing an explicit reference for the closure parameter _v: &_
solves the issue, but shouldn't it be inferred automatically?
Is it some bug/limitation of the borrow checker? Or I'm missing something more fundamental here?