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

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

问题描述

我编写了以下函数来比较两个迭代器,逐个元素。但是,如果我可以在标准库中重复使用它,那将会很棒。

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 等)。

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

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