如何Option从调用者的特定生命周期中提取引用并将其传回?
具体而言,我想借用参照Box<Foo>从Bar一个具有Option<Box<Foo>>在其中.我以为我能做到:
impl Bar {
fn borrow(&mut self) -> Result<&Box<Foo>, BarErr> {
match self.data {
Some(e) => Ok(&e),
None => Err(BarErr::Nope),
}
}
}
Run Code Online (Sandbox Code Playgroud)
......但结果是:
error: `e` does not live long enough
--> src/main.rs:17:28
|
17 | Some(e) => Ok(&e),
| ^ does not live long enough
18 | None => Err(BarErr::Nope),
19 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the …Run Code Online (Sandbox Code Playgroud) rust ×1