【发布时间】:2018-02-18 01:30:17
【问题描述】:
这里是完整的 Rust 示例: https://play.rust-lang.org/?gist=0778e8d120dd5e5aa7019bc097be392b&version=stable
一般的想法是实现一个通用的拆分迭代器,它将为每个由指定分隔符拆分的值生成迭代器。所以对于[1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9],split(0),你会得到[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
对于此代码:
impl<'a, I, F> Iterator for Split<I, F>
where I: Iterator,
F: PartialEq<I::Item>,
{
type Item = SplitSection<'a, I, F>;
fn next(&'a mut self) -> Option<Self::Item> {
self.iter.peek().map(|_|
SplitSection {
exhausted: false,
iter: self,
})
}
}
我收到以下错误:
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> src/main.rs:22:6
|
22 | impl<'a, I, F> Iterator for Split<I, F>
| ^^ unconstrained lifetime parameter
有没有办法“约束”生命周期参数,或者以某种方式对其进行重构,以便关联类型 (Item) 的返回生命周期将其绑定回 next()?
基本上,由于每个 SplitSection 都使用 Split 拥有的迭代器,所以我想确保两个 SplitSection 不会一次迭代。
谢谢!
【问题讨论】:
标签: rust