【问题标题】:ref to 'static does not live long enough?参考“静态”的寿命不够长?
【发布时间】: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


【解决方案1】:

想象一下,如果我决定调用函数 owned&lt;'static, i32&gt;(Vec&lt;i32&gt;, foo) 并将 foo 定义为:

fn foo(vec: &'static Vec<i32>) -> i32 { ... }

这满足owned 的界限,因为i32: 'static。然而,这意味着您必须有一个静态引用来调用f,但owner 不会永远存在,因为它在owned 结束时被销毁。

修复它的一种方法是使用以下方法:

fn owned<R>(owner: Vec<i32>, f: for<'a> fn(&'a Vec<i32>) -> R) {
    let _ = get_ref(&owner, f);
}

它说f 必须可以在 any 生命周期内调用,而不仅仅是某个特定的生命周期。但是,这会导致R 不能从参数中借用,因为R 的声明范围比'a 更大。在保持泛型不变的同时,没有任何方法可以解决这个问题。


这个答案来自我在 URLO 上对this thread 的回复。

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多