Curriculum Collections & iteration Iterators
Iterators
Idiomatic Rust often transforms data with iterators instead of manual indexing. The key ownership distinction is .iter(), .iter_mut(), and .into_iter(). Planned exercises: 1. choose .iter() for reading. 2. recognise .into_iter() moves. 3. choose .map(...).collect(). 4. fill .filter. 5. fill .sum::<i32>(). 6. write a map-collect. 7. write an iterator chain that borrows. 8. translate TS map and filter. 9. fix a chain that consumes a vector too early.
Exercises
9 ready
01
pick one
A function reads each element of a Vec<i32> to compute a sum,
02
pick one
rustc rejects this program with `error[E0382]: borrow of moved
03
pick one
Convert vec![1, 2, 3] into a new Vec<i32> with each element
04
fill blanks
Fill the iterator adapter that keeps only elements satisfying
05
fill blanks
Fill the part of the chain that tells .sum() what numeric
06
write a program
Build a new Vec<i32> from vec![1, 2, 3] by SQUARING each
07
write a program
Write sum_of_positives(xs: &[i32]) -> i32 that sums only the
08
write a program
Translate the TS array chain. Keep only positive elements,
09
write a program
This program tries to build doubled AND read sum, but