请考虑以下尝试来制作函数备注(playground link):
use std::collections::HashMap;
pub struct Cacher<F, IN, OUT> {
function: F,
cache: HashMap<IN, OUT>,
}
impl<F, IN, OUT> Cacher<F, IN, OUT>
where
F: Fn(IN) -> OUT,
IN: std::cmp::Eq + std::hash::Hash + std::marker::Copy,
OUT: std::marker::Copy,
{
fn new(function: F) -> Cacher<F, IN, OUT> {
Cacher {
function: function,
cache: HashMap::new(),
}
}
fn at(&mut self, arg: IN) -> &OUT {
match self.cache.get(&arg) {
None => {
let val = (self.function)(arg);
self.cache.insert(arg, val);
&val // FAIL!
}
Some(val) => val,
}
}
}
at
方法无法编译:
error[E0597]: `val` does not live long enough
--> src/lib.rs:26:18
|
26 | &val // FAIL!
| ^^^ borrowed value does not live long enough
27 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 21:5...
--> src/lib.rs:21:5
|
21 | / fn at(&mut self, arg: IN) -> &OUT {
22 | | match self.cache.get(&arg) {
23 | | None => {
24 | | let val = (self.function)(arg);
... |
29 | | }
30 | | }
| |_____^
error[E0502]: cannot borrow `self.cache` as mutable because it is also borrowed as immutable
--> src/lib.rs:25:17
|
22 | match self.cache.get(&arg) {
| ---------- immutable borrow occurs here
...
25 | self.cache.insert(arg, val);
| ^^^^^^^^^^ mutable borrow occurs here
...
30 | }
| - immutable borrow ends here
我的理解是,在必须计算值的情况下,返回的&val
寿命不长。
我尝试了一些操作,向编译器解释了返回的&val
应当与包含它的Cacher
一样长寿,但是到目前为止,我还没有成功。
我们如何为编译器提供有关&val
生命周期的必需信息?