What is the difference between len () and count ()?

In the code below, I get the same result whether I use lenor count:

fn main() {
    let vector = [0, 1, 2];
    assert_eq!(vector.iter().count(), vector.iter().len());
}

len seems to be more general as I can also do this:

assert_eq!(vector.len(), 3);

So what is the difference between the two ... why use one and not the other?

+4
source share
1 answer

vector.len ()

Returns the number of elements in a vector.

iter.len ()

Returns the exact length of the iterator.

iter.count ()

Counts the number of elements in this iterator.

, , count . , len ExactSizeIterator; , , , .

+5

All Articles