有没有内置的方法来比较两个迭代器?

Is there a built-in way to compare two iterators?

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

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 as well as various other comparison functions (lt, ne等)。

第三方 crate itertools has itertools::equal and itertools::assert_equal