Rust Exercises for learning to use std::iter::Iterator
If you already know the basics of Rust and would like to learn more about Rust iterators, these exercises are for you.
If you're just getting started with Rust, you should definitely do the Rustlings exercises (https://github.com/rust-lang/rustlings) first, and then come back and do these ones.
Each exercise starts with a function that already succeeds at accomplishing some task. Your goal is to use std::iter::Iterator
methods to write a more concise function that exactly matches the original function's behavior.
When you're done, you will have tried roughly half of Iterator
's methods (it's a long list!), and you will have a sense of how concise and elegant Rust iterators can be.
Start by reading
- The iterator chapter in the book
- The
std::iter
module documentation - and keep the
std::iter::Iterator
trait documentation open: you'll be looking through this a lot!
These exercises are test-driven: an exercise is complete when its tests pass. Run cargo test
to see the next error to be fixed, or to confirm your solution.
To advance to the next level, uncomment the next module in lib.rs
.
If you don't have Rust installed locally, you can also manually copy the exercise file contents in the src
directory to the Rust Playground and run them there.
ANSWERS.md
contains example answers for each exercise.