Rust iterator exercises
Rust iterators are zero-cost abstractions, but they are lazy: calling .map() without .collect() or a consuming loop executes nothing. Furthermore, indexing a Rust String with byte offsets panics if it splits a multi-byte UTF-8 character.
Solve practical Rust iterator and string processing bugs.
The iterators bugs you will meet
- Iterators are lazy: .map() or .filter() without a consumer like .collect() or for-loop does nothing.
- Slicing a String with &s[0..n] panics if index n is inside a multi-byte UTF-8 code point; use .chars() instead.
- Calling .unwrap() on an Option from .find() or .next() panics when no match exists; use unwrap_or or match.
- into_iter() consumes the collection, whereas .iter() borrows immutable references.
The exercises
Easy
Medium
- Iterator Error in Vector ProcessingScope and closure
- Array Indexing Error in Iterator Sum CalculationLogic
- Unexpected empty output from a Rust function using.map()Logic
- Immutable reference error during vector mutationScope and closure
- Indexing fails on preallocated but empty Rust vectorLogic
- Variable inaccessible after closure use in RustScope and closure
- Cursor position at EOF returns zero bytesLogic
- Borrow checker vector mutation during element iterationScope and closure
Errors you might be searching for
More JavaScript practice by topic
Questions
Why can't I index a Rust String like s[0]?
Rust Strings are strictly UTF-8 encoded byte arrays. Indexing by character would be O(N) rather than O(1), and byte indexing could bisect a unicode character.