I have a very simple piece of code that I cannot get to compile:
struct X;
struct A {
field: Option<Box<X>>,
}
impl A {
pub fn get_field(&self) -> Option<&X> {
return self.field.map(|value| &*value);
}
}
error[E0507]: cannot move out of borrowed content
--> src/lib.rs:9:16
|
9 | return self.field.map(|value| &*value);
| ^^^^ cannot move out of borrowed content
error[E0597]: `*value` does not live long enough
--> src/lib.rs:9:40
|
9 | return self.field.map(|value| &*value);
| ^^^^^-
| | |
| | borrowed value only lives until here
| borrowed value does not live long enough
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 8:5...
--> src/lib.rs:8:5
|
8 | / pub fn get_field(&self) -> Option<&X> {
9 | | return self.field.map(|value| &*value);
10| | }
| |_____^
I don't really understand why this doesn't work.