参考'静态的寿命不够长?

问题描述

考虑下一个代码:

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,6];
    owned(v,|x| x.iter().skip(1).take(2));
}

对我来说,它看起来非常相似。但是 Rust 无法编译它。我真的不明白为什么会发生这种情况,我应该如何重写代码以进行编译。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)