我有这个警告,但我不知道如何修复这个警告.

warning: method `next` can be confused for the standard trait method `std::iter::Iterator::next`
  --> src/util/mod.rs:51:5
   |
51 | /     pub fn next(&mut self) -> Option<I::Item> {
52 | |         if self.iterator.peek().is_some() {
53 | |             self.prev = replace(&mut self.current, self.iterator.next());
54 | |             return self.current.clone();
...  |
59 | |         None
60 | |     }
   | |_____^
   |
   = help: consider implementing the trait `std::iter::Iterator` or choosing a less ambiguous method name
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
   = note: `#[warn(clippy::should_implement_trait)]` on by default

这是我现在拥有的代码--一个能够窥视前一项的迭代器.next方法也在代码片段中.

pub struct PrevPeekable<I>
where
    I: Iterator,
    <I as Iterator>::Item: Clone,
{
    /// Iterator that `PrevPeekable` wraps
    iterator: Peekable<I>,
    /// The item before the current item
    prev: Option<I::Item>,
    /// The current item
    current: Option<I::Item>,
    /// Keeps track of whether the iterator has reached the end or not
    finished: bool,
}

impl<I> PrevPeekable<I>
where
    I: Iterator,
    <I as Iterator>::Item: Clone,
{
        /// Returns the next item in the iterator
    pub fn next(&mut self) -> Option<I::Item> {
        if self.iterator.peek().is_some() {
            self.prev = replace(&mut self.current, self.iterator.next());
            return self.current.clone();
        } else if !self.finished {
            self.prev = replace(&mut self.current, self.iterator.next());
            self.finished = true;
        }
        None
    }
}

先谢谢你.

推荐答案

您需要实现Iterator特征,并将您的next代码移到此实现中:

use std::mem::replace;
use std::iter::Peekable;

pub struct PrevPeekable<I>
where
    I: Iterator,
    <I as Iterator>::Item: Clone,
{
    /// Iterator that `PrevPeekable` wraps
    iterator: Peekable<I>,
    /// The item before the current item
    prev: Option<I::Item>,
    /// The current item
    current: Option<I::Item>,
    /// Keeps track of whether the iterator has reached the end or not
    finished: bool,
}

impl<I> Iterator for PrevPeekable<I>
where
    I: Iterator,
    <I as Iterator>::Item: Clone,
{
    type Item = I::Item;

    /// Returns the next item in the iterator
    fn next(&mut self) -> Option<I::Item> {
        if self.iterator.peek().is_some() {
            self.prev = replace(&mut self.current, self.iterator.next());
            return self.current.clone();
        } else if !self.finished {
            self.prev = replace(&mut self.current, self.iterator.next());
            self.finished = true;
        }
        None
    }
}

Playground

Rust相关问答推荐

展开枚举变量并返回所属值或引用

Rust TcpStream不能在读取后写入,但可以在不读取的情况下写入.为什么?

修改切片/引用数组

如何在Tauri中将变量从后端传递到前端

如何导入crate-type=[";cdylib;]库?

S在Cargo.toml中添加工作空间开发依赖关系的正确方法是什么?

在我的Cargo 中,当我在建筑物中使用时,找不到我可以在产品包中使用的 crate .r我如何解决这个问题?

如何在Rust中将选项<;选项<;字符串>;转换为选项<;选项&;str>;?

在Rust内联程序集中使用字符串常量

我可以禁用发布模式的开发依赖功能吗?

trait 对象指针的生命周期

从Rust 的临时文件中创建引用是什么意思?

在不安全的 Rust 中存储对 struct 内部数据的静态引用是否合法?

仅在运行测试时生成调试输出

Rust HRTB 是相同的,但编译器说一种类型比另一种更通用

如何在 nom 中构建负前瞻解析器?

tokio async rust 的 yield 是什么意思?

如何从 many0 传播 Nom 失败上下文?

如何制作具有关联类型的特征的类型擦除版本?

为什么我返回的 impl Trait 的生命周期限制在其输入的生命周期内?