Skip to content

Commit 9a0eb7d

Browse files
committed
borrowing: 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 9a0eb7d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/borrowing/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 ../../third_party/rust-on-exercism/health-statistics.rs:solution}}
55
```
6+
7+
- **Lifetimes in Structs:** `HealthReport` has a lifetime parameter `'a` because
8+
it contains a reference `patient_name: &'a str`. This ensures that the report
9+
cannot outlive the `User` it refers to.
10+
- **Mutable Reference (`&mut self`):** `visit_doctor` modifies the `User`
11+
struct, so it must take `&mut self`.
12+
- **Lifetime Elision:** The return type `HealthReport<'_>` indicates that the
13+
output lifetime is tied to the input lifetime of `self`. Explicitly, this
14+
would be `fn visit_doctor<'a>(&'a mut self, ...) -> HealthReport<'a>`.
15+
- **Option combinators:** We use `self.last_blood_pressure.map(...)` to
16+
convenienty calculate the blood pressure change if the previous measurement
17+
exists.
18+
19+
<details>
20+
21+
- Explain that `HealthReport` borrows from `User`. While `report` exists, `User`
22+
is borrowed (mutably, because it came from `visit_doctor`), so we cannot use
23+
`User` for anything else until `report` is dropped.
24+
- Note the cast to `i32` for blood pressure calculation to allow for negative
25+
changes.
26+
27+
</details>

0 commit comments

Comments
 (0)