有没有比较两个迭代器的内置方法? [英] Is there a built-in way to compare two iterators?

查看:12
本文介绍了有没有比较两个迭代器的内置方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下函数来逐个元素地比较两个迭代器.但是,如果我可以重用标准库中的某些内容,那就太好了.

I've written the following function to compare two iterators, element-by-element. However, it would be great if I could just reuse something in the standard library.

fn iter_eq<A, B, T, U>(mut a: A, mut b: B) -> bool
where
    A: Iterator<Item = T>,
    B: Iterator<Item = U>,
    T: PartialEq<U>,
{
    loop {
        match (a.next(), b.next()) {
            (Some(ref a), Some(ref b)) if a == b => continue,
            (None, None) => return true,
            _ => return false,
        }
    }
}

fn main() {
    let a = vec![1, 2, 3].into_iter();
    let b = vec![1, 2, 3].into_iter();

    assert!(iter_eq(a, b));
}

推荐答案

这里有 Iterator::eq 以及其他各种比较函数(lt, ne 等).

There's Iterator::eq as well as various other comparison functions (lt, ne, etc.).

这篇关于有没有比较两个迭代器的内置方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆