How to iterate over a slice of displayed fragments?
Here's an example of what I'm trying to do:
for &xs in &[&[1, 2, 3].iter().map(|x| x + 1)] {
for &x in xs {
println!("{}", x);
}
}
This gives me the following error:
error[E0277]: the trait bound `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:2:40: 2:49]>: std::iter::Iterator` is not satisfied --> src/main.rs:3:9 | 3 | / for &x in xs { 4 | | println!("{}", x); 5 | | } | |_________^ the trait `std::iter::Iterator` is not implemented for `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:2:40: 2:49]>` | = note: `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:2:40: 2:49]>` is not an iterator; maybe try calling `.iter()` or a similar method = note: required by `std::iter::IntoIterator::into_iter`
... which is very surprising, because I can clearly see how it std::Iter::Map
implementsIterator
.
Why is he complaining and how to iterate over a slice of displayed fragments?
source to share
&T
cannot be repeated as next
mutates.
Thus, if you have &Map<_, _>
, you cannot repeat it.
You may not understand what it &[1,2,3].iter().map(|&x| x+1)
means
&([1,2,3].iter().map(|&x| x+1))
giving a link.
Usage will for &xs in &[&mut ...]
also not work as it requires a move xs
from an immutable reference. There is currently also no iterator by value over fixed length arrays. I believe the easiest solution is
for xs in &mut [&mut [1, 2, 3].iter().map(|&x| x+1)] {
for x in xs {
println!("{}", x);
}
}
Note that this also requires fixing an issue with a call map
that did not dereference its input.
source to share