Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/references/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
{{#include exercise.rs:solution}}
```

This solution highlights the difference between shared and mutable references:

- **Shared References (`&`):** `magnitude` needs to read the vector components
but not modify them, so it takes a shared reference (`&[f64; 3]`).
- **Mutable References (`&mut`):** `normalize` modifies the vector in place, so
it requires an exclusive mutable reference (`&mut [f64; 3]`).
- **Dereferencing:** In the `normalize` loop, `item` is a `&mut f64`. To modify
the actual value, we must dereference it using `*item`.
- **Iteration:** When we iterate over a reference to an array (like `vector`),
the iterator yields references to the elements. In `magnitude`, `coord` is
`&f64`. In `normalize`, `item` is `&mut f64`.

<details>

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