- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个解析器,需要从迭代器进行前瞻,但不想使用 Peekable
特性。相反,我为迭代器使用了一个包装器。我想出了这样的东西,省略了所有不必要的东西:
struct Wrapper<'a> {
it: &'a mut Iterator<Item = &'a String>,
}
pub trait DoSomething {
fn do_something(self);
}
impl<'a, T> DoSomething for T
where
T: IntoIterator<Item = &'a String>,
{
fn do_something(self) {
let mut it = self.into_iter();
let throwaway = Wrapper { it: &mut it };
}
}
编译失败:
error[E0309]: the associated type `<T as std::iter::IntoIterator>::IntoIter` may not live long enough
--> src/main.rs:15:39
|
15 | let throwaway = Wrapper { it: &mut it };
| ^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as std::iter::IntoIterator>::IntoIter: 'a`...
note: ...so that the type `<T as std::iter::IntoIterator>::IntoIter` is not borrowed for too long
--> src/main.rs:15:39
|
15 | let throwaway = Wrapper { it: &mut it };
| ^^^^^^^
error[E0309]: the associated type `<T as std::iter::IntoIterator>::IntoIter` may not live long enough
--> src/main.rs:15:39
|
15 | let throwaway = Wrapper { it: &mut it };
| ^^^^^^^
|
= help: consider adding an explicit lifetime bound `<T as std::iter::IntoIterator>::IntoIter: 'a`...
note: ...so that the type `<T as std::iter::IntoIterator>::IntoIter` will meet its required lifetime bounds
--> src/main.rs:15:39
|
15 | let throwaway = Wrapper { it: &mut it };
| ^^^^^^^
虽然我不明白为什么这是必要的(问题 1),但我在 impl
的 where 子句中添加了 T::IntoIter: 'a
。这失败了:
error[E0597]: `it` does not live long enough
--> src/main.rs:16:44
|
16 | let throwaway = Wrapper { it: &mut it };
| ^^ borrowed value does not live long enough
17 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 9:1...
--> src/main.rs:9:1
|
9 | / impl<'a, T> DoSomething for T
10 | | where
11 | | T: IntoIterator<Item = &'a String>,
12 | | T::IntoIter: 'a,
... |
17 | | }
18 | | }
| |_^
我也不明白为什么 it
的生命周期不够长,因为释放顺序应该是 throwaway
然后是 it
。使用第二个生命周期 'b
也不起作用,无论是 'a: 'b
还是 'b: 'a
(我很沮丧并尝试了所有组合)。
唯一有帮助的是将 Iterator 引用及其包含的引用的生命周期分开并将它们关联起来(无需指定 T::IntoIter
的生命周期):
struct Wrapper<'a, 'b: 'a> {
it: &'a mut Iterator<Item = &'b String>,
}
为什么?
说“项目引用必须至少与迭代器引用一样长”是有道理的,但我不明白的是为什么它们不能相同以及为什么错误消息暗示Wrapper
构造而不是定义,其中更改生命周期根本没有帮助。
我发现有关生命周期的官方文档非常困惑。如果生命周期注解实际上改变了编译代码中关于释放的任何东西,或者它是否只是帮助静态分析而不实际改变一 block 内存的实际生命周期,它没有涉及。
最佳答案
&'a mut Iterator<Item = &'a String>
意味着对迭代器的引用和迭代器返回的引用的生命周期必须能够统一(到 'a
)。
声明迭代器和迭代器的引用具有不同的生命周期允许代码编译:
struct Wrapper<'i, 's: 'i> {
it: &'i mut Iterator<Item = &'s String>,
}
无需添加 T::IntoIter: 'a
.
另见:
关于rust - 为什么代码编译需要这些确切的生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759319/
我正在开发一个使用多个 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
我是一名优秀的程序员,十分优秀!