Skip to content

Commit 5beaad8

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 5beaad8

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/iterators/solution.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,25 @@
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
12+
`b`. The `zip` function yields pairs of elements. Crucially, `zip` stops as
13+
soon as either iterator is exhausted. Since `a` has the length of `values` and
14+
`b` is infinite, the resulting zipped iterator has the same length as
15+
`values`.
16+
- **`map()` and `collect()`:** We calculate the difference for each pair and
17+
collect the results into a new `Vec`.
18+
19+
<details>
20+
21+
- Mention that `values.iter()` creates an iterator that yields references
22+
(`&i32`). In the `map` closure, we dereference them (`*b - *a`) to perform
23+
arithmetic.
24+
- This functional approach is often more concise and less error-prone (no
25+
off-by-one index errors) than manual looping with indices.
26+
27+
</details>

0 commit comments

Comments
 (0)