I don’t understand the size_hint() basic usage example

Hi,
I’m puzzled by an example in the std doc:

The basic example create an iterator from an array, so far so good. But the lower bound of the iterator is not 0 but 3. Why?

Why should it be 0? Slices are of a definite size, slice iterators know exactly how many elements they will yield. An iterator over a slice of length 3 will always yield precisely 3 elements, so both the lower and the upper bound on the length are 3.

OK, english is not my main language, so maybe my issue here it with the word "bound". I though it was equivalent to index in an array

The size hint returns the size (length, i.e., number of elements) of the iterator: the minimal and maximal possible number of elements (which may not coincide if the iterator doesn't know exactly how many elements it will yield, e.g. filter()).

It does not return the range of indices. First of all, indices would not be well-defined, because not nearly all iterators come from arrays. Second, if they were, then the first value would be useless, since, by your logic, it would always need to be zero.

3 Likes

Ta, it makes sense know :smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.