4 ms·
No, you definitely can. You generally can't mutate things that would cause the iterator itself to be invalidated (like, you can't delete items from a vector whi
by apendleton 4y ago
No, you definitely can. You generally can't mutate things that would cause the iterator itself to be invalidated (like, you can't delete items from a vector while you're iterating over it), but you can get an iterator over a vector that gives you a mutable reference to each of its members, which would be sufficient to do what you're suggesting here.
I think what they were pointing out (and I agree with) is that the mixing of combinators and imperative-style for loops that's going on in the post doesn't feel very natural. Typically if you would _either_ do
blah
.into_iter()
.filter(|x| x.some_condition())
.for_each(|x| x.do_something());
or
for x in blah {
if !x.some_condition() {
continue;
}
x.do_something();
}
But `for x in blah.into_iter().filter(...` is a bit unusual.