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

vector.len()

Returns the number of elements in the vector.

iter.len()

Return the exact length of the iterator.

iter.count()

Counts the number of elements in this iterator.

So while they return the same value, count will actually count the elements. Note that len is available only for ExactSizeIterator; so if the value is lazy-retrieved the total length may not be available and you need to explicitly count it.

Leave a Comment