Skip to content

Commit 5af29e0

Browse files
committed
references: 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 5af29e0

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/references/solution.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
{{#include exercise.rs:solution}}
55
```
66

7+
This solution highlights the difference between shared and mutable references:
8+
9+
- **Shared References (`&`):** `magnitude` needs to read the vector components
10+
but not modify them, so it takes a shared reference (`&[f64; 3]`).
11+
- **Mutable References (`&mut`):** `normalize` modifies the vector in place, so
12+
it requires an exclusive mutable reference (`&mut [f64; 3]`).
13+
- **Dereferencing:** In the `normalize` loop, `item` is a `&mut f64`. To modify
14+
the actual value, we must dereference it using `*item`.
15+
- **Iteration:** When we iterate over a reference to an array (like `vector`),
16+
the iterator yields references to the elements. In `magnitude`, `coord` is
17+
`&f64`. In `normalize`, `item` is `&mut f64`.
18+
719
<details>
820

921
- Note that in `normalize` we were able to do `*item /= mag` to modify each

0 commit comments

Comments
 (0)