Just knowing they're both Iterator
without knowing Iterator
s type parameter doesn't tell you anything about what they're iterating over, which is the important difference here. For this code:
range1
is anIterator<int>
range2.iter()
is anIterator<&int>
That is, slice iterator returns references pointing directly into the slice (it has to do this or, for general T
, it would be trying to steal ownership out of a borrowed slice &[T]
: illegal), while range
just returns the counter by-value since it constructs it internally, and is free to hand it out ("transferring ownership").
Hence, to get an int
out of the slice iterator, you need to dereference the reference somehow, e.g. pattern matching through it via |&x| ...
, or writing |x| *x * *x
.