Skip to content

Commit d756952

Browse files
committed
iterators: add explanatory commentary to solution
This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts.
1 parent 0867e24 commit d756952

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/iterators/solution.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,23 @@
33
```rust,editable
44
{{#include exercise.rs:solution}}
55
```
6+
7+
- **`cycle()`:** We create an iterator `b` that repeats the elements of `values`
8+
infinitely. This handles the wraparound requirement elegantly without manual
9+
index modulo arithmetic.
10+
- **`skip()`:** We advance the cycled iterator by `offset` positions.
11+
- **`zip()`:** We combine the original iterator `a` with the offset iterator `b`.
12+
The `zip` function yields pairs of elements. Crucially, `zip` stops as soon as
13+
either iterator is exhausted. Since `a` has the length of `values` and `b` is
14+
infinite, the resulting zipped iterator has the same length as `values`.
15+
- **`map()` and `collect()`:** We calculate the difference for each pair and collect
16+
the results into a new `Vec`.
17+
18+
<details>
19+
20+
- Mention that `values.iter()` creates an iterator that yields references (`&i32`).
21+
In the `map` closure, we dereference them (`*b - *a`) to perform arithmetic.
22+
- This functional approach is often more concise and less error-prone (no off-by-one
23+
index errors) than manual looping with indices.
24+
25+
</details>

0 commit comments

Comments
 (0)