- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想写一个函数 A,它以一个函数 B 作为参数,该函数以一个类型作为参数,该类型由一个引用类型参数化,该引用类型的生命周期至少是 A 主体中局部变量的生命周期。
考虑以下示例:
struct Foo {}
fn consume(mut v: Vec<&Foo>) {
while let Some(..) = v.pop() {
// Do stuff
continue;
}
}
fn setup_and<F>(f: F)
where
F: FnOnce(&mut Vec<&Foo>) + Send,
{
let mut v: Vec<&Foo> = vec![];
let other_foo = Foo {};
f(&mut v);
v.push(&other_foo);
consume(v);
}
fn main() {
let foo = Foo {};
setup_and(|v| {
v.push(&foo);
});
}
rustc 无法自行推断生命周期。它提示:
error[E0597]: `foo` does not live long enough
--> src/main.rs:25:17
|
24 | setup_and(|v| {
| --- value captured here
25 | v.push(&foo);
| --------^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `foo` is borrowed for `'static`
26 | });
27 | }
| - `foo` dropped here while still borrowed
我尝试为 setup_and
所采用的引用指定生命周期,如下所示:
fn setup_and<'a, F>(f: F)
where
F: FnOnce(&mut Vec<&'a Foo>) + Send,
{
let mut v: Vec<&'a Foo> = vec![];
现在 rustc 提示 setup_and
本地引用 other_foo
活得不够长。我认为这是因为它需要比 setup_and
范围更长的生命周期。
在这种情况下,我该如何正确绑定(bind)生命周期?我想表达的是,引用必须在 consume
调用结束之前有效。
最佳答案
您的实现中存在生命周期冲突的严重问题,如果不至少部分重新设计 struct
和方法的外部签名,就没有简单的解决方法。它们都源于 setup_and
方法,并在您明确描述生命周期边界时由编译器突出显示。
下面复制了您的方法主体,并附有注释以供您理解问题:
let mut v: Vec<&Foo> = vec![];
let other_foo = Foo {}; // other_foo is created here
f(&mut v);
v.push(&other_foo); // other_foo requires lifetime 'a to be added to this
consume(v); // consume does not restrict the lifetime requirement 'a
// other_foo is dropped here, at lifetime less than 'a
这个问题最简单的解决方案是存储一个 Arc<Foo>
,就像这样 ( playground ):
fn setup_and<F>(f: F)
where
F: FnOnce(&mut Vec<Arc<Foo>>) + Send,
{
let mut v: Vec<Arc<Foo>> = vec![];
let other_foo = Foo {};
f(&mut v);
v.push(Arc::new(other_foo));
consume(&mut v);
}
Arc
是一个原子引用计数指针。它是一个可克隆结构,用作指向堆上对象的动态指针;出于所有意图和目的,它只作为只读引用使用,不需要终身使用。当 Arc
的所有副本都被删除时,其中的项目也会被删除。
这解决了两个问题:
other_foo
现在已移到 Arc
中,不再导致其生命周期问题Arc
实现 Deref
)之所以选择 Arc
,是因为您的 FnOnce
需要 Send
,而 Rc
(Arc
的单线程变体)无法提供。
关于rust - 如何将引用生命周期绑定(bind)到函数局部范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184443/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!