IntoIterator (implemented for &[T; N] and &mut [T; N])
Arrays coerce to slices ([T]), so a slice method may be called on an array.
fn main() {
let arr = [1,2,3,4];
for v in arr.into_iter() {
println!("{}", v);
}
}
Initially I am thinking arr is coerced to [T] and calling into_iter() on [T]. However, per documentation, IntoIterator is only implemented for &'a [T] and &'a mut [T] , there is no way into_iter() was called on [T].
The author of the Rust by Example post illustrates the surprise coming from the dependence on the context (i.e., the type) on which into_iter is called, and is also compounding the problem by using the fact that:
IntoIterator is not implemented for [T; N] , only for &[T; N] and &mut [T; N]
When a method is not implemented for a value, it is automatically searched for references to that value instead
which is very surprising for into_iter since all types (except [T; N] ) implement it for all 3 variations (value and references). It's not possible for the array to implement an iterator that yields values because it cannot "shrink" to give up its items
You should also avoid calling .into_iter() on an array because the standard library is allowed to introduce impl<T> IntoIterator for [T; _] in the future which will change the behaviour of this code (and the more code in the wild using this the harder it will be to introduce).
Luckily clippy has a lint denying this usage, that will hopefully be ported into rustc at some point:
error: this .into_iter() call is equivalent to .iter() and will not move the array
--> src/main.rs:4:18
|
4 | for v in arr.into_iter() {
| ^^^^^^^^^ help: call directly: `iter`
|
= note: `#[deny(clippy::into_iter_on_array)]` on by default
(This can get even more complicated as &[T; 257]doesn't implement IntoIterator yet the example code also works for it. There's another step to method resolution which includes unsized coercions and finds the slice implementation impl<T> IntoIterator for &[T] and chooses to use that. Interestingly that one is just a warning in clippy rather than an error.)