Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing iterator with (fixed sized) array

I built an iterator that generates an infinite list of primes. The types look like this:

pub struct Primes { … }

impl Iterator for Primes {
    type Item = u32;
    fn next(&mut self) -> Option<Self::Item> { … }
}

Now I want to test if my iterator returns the correct values by comparing the first 100 values against the correct ones:

#[test]
fn first_thousand() {
    assert_eq!(
        Primes::new().take(100),
        first_100_primes
    );
}

const first_100_primes: [u32; 100] = [2, 3, …, 541];

I do not know how to compare these values. I tried creating a slice (first_100_primes[..]), collecting the iterator values, but I don't seem to be able to compare them.

like image 283
Sebastian Avatar asked May 11 '26 13:05

Sebastian


1 Answers

let is_correct = Primes::new()
    .zip(FIRST_100_PRIMES.iter())
    .all(|(a, &b)| a == b);
assert!(is_correct);
like image 133
A.B. Avatar answered May 14 '26 01:05

A.B.