Description
This problem comes up in generic contexts, but the simplest code to reproduce is:
let iter = &[1,2,3].iter();
for i in iter {
}
Results in:
error[E0277]:
&std::slice::Iter<'_, {integer}>
is not an iterator| for i in iter { | ^^^^ `&std::slice::Iter<'_, {integer}>` is not an iterator |
= help: the trait
std::iter::Iterator
is not implemented for&std::slice::Iter<'_, {integer}>
= note: required bystd::iter::IntoIterator::into_iter
At first glance it looks wrong, because it says that Iter
is not an iterator. It is not obvious that immutability of the shared reference is what breaks the code.
Interestingly, an only a slightly different code produces a better suggestion:
let iter = [1,2,3].iter();
for i in &iter {
}
help: consider removing 1 leading
&
-references
So possibly the condition for the help text is too narrow, and could be extended to work in general case. It could also suggest using &mut
as another alternative.
(#37914 has a similar problem, but a different root cause)