With new rustc 1.53.0 IntoIterator for arrays, I naively tried this code and the errors message looks scary and for beginners might be very hard to understand.
Given the following code:
fn main() {
let vec = vec![10];
for i in vec.iter().chain([20]){
}
}
Compiling playground v0.0.1 (/playground)
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`
error: aborting due to 3 previous errors
Ideally the output should look like:
Thanks to @CAD97 for suggested output
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]) {}
^^^^^^^^^^^^
It's better to choose only one of those three idioms, and to only suggest that. (Personally I am slightly in preference for the third, with into_iter).
With new rustc 1.53.0 IntoIterator for arrays, I naively tried this code and the errors message looks scary and for beginners might be very hard to understand.
Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=be232fba39739b9e22a793b1c289f440
The current output is:
Ideally the output should look like:
Thanks to @CAD97 for suggested output
EDIT: URLO thread https://users.rust-lang.org/t/rust-1-53-intoiterator/61350