I'm having trouble with lifetimes in a program I'm writing. It boils down to this:
fn main() {
let x = 7;
let v = vec![1,2,3];
let c = {
let r = &x;
|y: u32| r + y + v[2]
};
println!("{} {}", v[1], c(2));
}
x
, v
and c
outlive the scope in which r
is defined. x
and v
both outlive c
. Let's suppose that x
is not directly accessible and that we only have the reference r
. In the closure, I want to reference x
.
I get the following error:
error[E0373]: closure may outlive the current block, but it borrows `r`, which is owned by the current block
--> test.rs:6:9
|
6 | |y: u32| r + y + v[2]
| ^^^^^^^^ - `r` is borrowed here
| |
| may outlive borrowed value `r`
|
note: block requires argument type to outlive `'1`
--> test.rs:4:9
|
4 | let c = {
| ^
help: to force the closure to take ownership of `r` (and any other referenced variables), use the `move` keyword
|
6 | move |y: u32| r + y + v[2]
|
Apparently r
is borrowed, when in reality I want r
's underlying pointer to be copied so that I borrow x
. Note that I cannot follow the compilers advice, as v
must not be moved.
How can I get the closure to borrow x
instead or r
?