以下代码无法编译,因为闭包传递给map
,它借用了封闭函数(cache
)的参数,显然比封闭函数更长。
use std::collections::HashMap;
fn main() {
let mut data = Vec::new();
let mut foo_cache = HashMap::new();
let foo = get_foo(7, &mut data, &mut foo_cache);
}
struct Foo<'a> {
n: &'a u8,
}
fn get_foo<'a, 'b>(n: u8,
data: &'a mut Vec<u8>,
cache: &'b mut HashMap<u8, Foo<'a>>)
-> Option<&'b Foo<'a>> {
if let Some(x) = cache.get(&n) {
Some(x)
} else {
make_foo(n, data).map(|f| {
cache.insert(n, f);
&cache[&n]
})
}
}
fn make_foo<'a>(n: u8, data: &'a mut Vec<u8>) -> Option<Foo<'a>> {
if n % 2 != 0 {
None
} else {
data.push(n);
Some(Foo { n: &data[data.len() - 1] })
}
}
错误:
error[E0373]: closure may outlive the current function, but it borrows `cache`, which is owned by the current function
--> src/main.rs:21:35
|
21 | make_foo(n, data).map(|f| {
| ^^^ may outlive borrowed value `cache`
22 | cache.insert(n, f);
| ----- `cache` is borrowed here
|
help: to force the closure to take ownership of `cache` (and any other referenced variables), use the `move` keyword, as shown:
| make_foo(n, data).map(move |f| {
我认为闭包可以立即调用,也可以根本不调用,并且get_foo
返回后不需要存在闭包。我怎么能告诉编译器这种情况呢?