【发布时间】:2021-02-20 17:22:28
【问题描述】:
考虑下一个代码:
fn get_ref<'a, R>(slice: &'a Vec<i32>, f: fn(&'a Vec<i32>) -> R) -> R
where
R: 'a,
{
f(slice)
}
fn main() {
let v = [1,2,3,4,5,6];
let iter = get_ref(&v, |x| x.iter().skip(1).take(2));
println!("{:?}", iter.collect::<Vec<_>>());
}
我创建了一些static 变量,然后将一些函数应用于它的引用并获得结果。
它似乎工作得很好。至少它编译成功了。
现在我正在尝试添加下一个抽象级别。事情变得越来越奇怪......
fn owned<'a, R>(owner: Vec<i32>, f: fn(&'a Vec<i32>) -> R)
where
R: 'a,
{
let _ = get_ref(&owner, f); // error occurs here
// `owner` does not live long enough.
}
// get_ref is the same as in the first example
fn get_ref<'a, R>(slice: &'a Vec<i32>, f: fn(&'a Vec<i32>) -> R) -> R
where
R: 'a,
{
f(slice)
}
fn main() {
let v = [1,2,3,4,5,6];
owned(v, |x| x.iter().skip(1).take(2));
}
对我来说,它看起来几乎是相同的代码。但是 Rust 无法编译它。我真的不明白为什么会这样,我应该如何重写我的代码来编译。
【问题讨论】:
-
在调用点选择泛型。虽然
O可能存在于'static,但它的借用只存在于函数的生命周期内,所以如果'a比函数的生命周期长,它就会失败,这就是你得到错误的原因。 -
那么为什么它在第一个示例中有效?
-
在第一个例子中,引用来自函数外部,这意味着借用可以超过函数的范围。
-
在第二个示例中,引用也来自函数外部。
owned拥有第二个示例中的值,就像main拥有第一个示例中的v。
标签: rust lifetime borrow-checker borrowing borrow