Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

iterator

rust

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));
}
like image 716
Shepmaster Avatar asked May 30 '15 00:05

Shepmaster


1 Answers

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

like image 74
huon Avatar answered Sep 20 '22 11:09

huon