You're creating a Vec<{integer}> in your topmost code example, not an array.
Furthermore, as indicated in the release notes, you'll have to use IntoIterator::into_iter(array) or for x in array {...} rather than for x in array.iter() {...} or for x in array.into_iter() {...} if you want to iterate over an array by value.
Vec<i32>::iter() returns impl Iterator<Item=&i32>, so extend expects impl IntoIterator<Item=&i32>. Similarly, [i32; N]::iter() still returns returns impl Iterator<Item=&i32>, so extend expects impl IntoIterator<Item=&i32>.
Your original example will work with vec.iter().copied() to get Iterator<Item=i32>, and your second example will work with IntoIterator::into_iter([10, 20]) to get Iterator<Item=i32>.
Mixing up iterators of T and oterators of &T is a common mistake. What exactly was the error you ran into, and how might we adjust the wording that could've allowed to to realize what you miscommunicated to the compiler easier?
error[E0271]: type mismatch resolving `<[{integer}; 1] as IntoIterator>::Item == &{integer}`
--> src/main.rs:3:25
|
3 | for i in vec.iter().chain([20]) {}
| ^^^^^ expected integer, found `&{integer}`
error[E0271]: type mismatch resolving `<std::array::IntoIter<{integer}, 1_usize> as Iterator>::Item == &{integer}`
--> src/main.rs:3:14
|
3 | for i in vec.iter().chain([20]) {}
| ^^^^^^^^^^^^^^^^^^^^^^ expected `&{integer}`, found integer
|
= note: required because of the requirements on the impl of `Iterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
= note: required because of the requirements on the impl of `IntoIterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
= note: required by `into_iter`
error[E0271]: type mismatch resolving `<std::array::IntoIter<{integer}, 1_usize> as Iterator>::Item == &{integer}`
--> src/main.rs:3:14
|
3 | for i in vec.iter().chain([20]) {}
| ^^^^^^^^^^^^^^^^^^^^^^ expected `&{integer}`, found integer
|
= note: required because of the requirements on the impl of `Iterator` for `std::iter::Chain<std::slice::Iter<'_, {integer}>, std::array::IntoIter<{integer}, 1_usize>>`
= note: required by `std::iter::Iterator::next`
There's definitely room for improvement here. Ideally, I'd like to see something like this instead:
error[E0271]: type mismatch resolving `<[{integer}; 1] as IntoIterator>::Item == &{integer}`
--> src/main.rs:3:25
|
3 | for i in vec.iter().chain([20]) {}
| ^^^^^ expected integer, found `&{integer}`
= help: try iterating by reference instead
for i in vec.iter().chain(&[20]) {}
^
= help: try copying the iterated items
for i in vec.iter().copied().chain([20]) {}
^^^^^^^^^
= help: try iterating by value instead
for i in vec.into_iter().chain([20]) {}
^^^^^^^^^^^^
The third suggestion is probably the hardest, but the first two should actually be somewhat reasonable. The other two errors are just rehashing the existing problem, in that the result of calling Chain is malformed, so ideally would be suppressed.
(I won't be able to open an issue for this for a while, so if someone else can, please do!)
Got it. Carefully reading the release notes, made it clear. Currently array.into_iter() and array.iter() are both resolve to &Item iterator for backward compatibility, and in the upcoming edition this will likely be changed so IntoIterator::into_iter(array) and array.into_iter() are equal.
Nitpick: the current, forward-compatible way to create an iterator of T from a [T; N] is std::array::IntoIter::new(array), not IntoIterator::into_iter(array). [ignore this, sorry]
Why the nitpick? Either way will work, and will continue to work in the future. You can even infer it with <_>::into_iter(array) if you want a little more brevity.
The only thing affected by the future is the method call syntax, array.into_iter().
Rustc switched from IntoIter::new to IntoIterator::into_iter, so I'd think the latter is preferred. Generally, IntoIter types shouldn't need to be referred to by name.
Using IntoIter directly was only necessary for the small number of releases before arrays were IntoIterator.
Which you use is a style preference for now. And in edition=2021+, .into_iter() will do the correct thing.