Today I learned iterators.
The part that finally clicked was ownership vs borrowing:
.iter() โ gives you &T (borrowed)
.iter_mut() โ gives you &mut T (borrowed, mutable)
.into_iter() โ gives you T (owned, consumes the collection)
So when I wrote:
let filtered: &Vec<Developer> = developers.iter().filter(...).collect();
Rust rejected it, and I finally understood why:
.collect() builds a brand new Vec, it can't hand you back a reference to the old one.
The fix was just the type:
Vec<&Developer>
A new vector, whose items happen to be references into the old one.
Also learned the hard way that .map() is for transforming values, not for mutating and discarding that's what .for_each() or a plain loop is for.
#rust #programming