首页 文章

相关类型的自我生命

提问于
浏览
1

完整的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拥有的迭代器,我想确保两个SplitSections不会一次迭代 .

谢谢!

1 回答

相关问题