Note that it's worse than that. A consumer of the iterator is allowed to misbehave (so long as it doesn't trigger UB) if the implementation doesn't respect the rules.
So, for example, if the size_hint
is (n, Some(n))
, it would be legal for collect
to notice that and run
let mut v = Vec::with_capacity(n);
for _ in 0..n {
v.push(iter.next().expect("look, you said it has this many"));
}
and thus end up panicking if the iterator didn't actually have that many items in it, or to silently drop omit some if there were actually more in the iterator than the upper bound said.
(TBH, I don't think size_hint
is the best API. filter
would also be happier with one that's more like capacity_guess
that's allowed to over- or under-shoot the real value arbitrarily. Especially since last I checked, size_hint().1
is never actually used for anything.)