For this code:
fn doit(data: &'static mut ()) {
|| doit(data);
}
(works fine if you drop the mut, still happens with a move closure).
The compiler (stable and nightly) spits out this error:
error[E0313]: lifetime of borrowed pointer outlives lifetime of captured variable `data`...
--> <anon>:3:13
|
3 | || doit(data);
| ^^^^
|
= note: ...the borrowed pointer is valid for the static lifetime...
note: ...but `data` is only valid for the lifetime as defined on the body at 3:7
--> <anon>:3:8
|
3 | || doit(data);
| ^^^^^^^^^^
error: aborting due to previous error
- The last note is broken:
for the lifetime as defined on the actual lifetime is invisible. There is also no lifetime defined in the entire program, so it's not clear which lifetime the note is talking about.
- The main error message is very unclear. Is it saying that
data itself must live as long as the data behind the reference? Why?
According to the rustc source code, this error is related to an implicit reborrow done by the closure. The error mentions nothing of that sort.
For this code:
(works fine if you drop the
mut, still happens with amoveclosure).The compiler (stable and nightly) spits out this error:
for the lifetime as defined onthe actual lifetime is invisible. There is also no lifetime defined in the entire program, so it's not clear which lifetime the note is talking about.dataitself must live as long as the data behind the reference? Why?According to the rustc source code, this error is related to an implicit reborrow done by the closure. The error mentions nothing of that sort.