From 9a0eb7d3d41964cf199af2a50febe1439bc9767a Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 12:59:35 +0100 Subject: [PATCH 1/2] 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. --- src/borrowing/solution.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/borrowing/solution.md b/src/borrowing/solution.md index 208f24d3d29d..92f766f14e4b 100644 --- a/src/borrowing/solution.md +++ b/src/borrowing/solution.md @@ -3,3 +3,25 @@ ```rust,editable {{#include ../../third_party/rust-on-exercism/health-statistics.rs:solution}} ``` + +- **Lifetimes in Structs:** `HealthReport` has a lifetime parameter `'a` because + it contains a reference `patient_name: &'a str`. This ensures that the report + cannot outlive the `User` it refers to. +- **Mutable Reference (`&mut self`):** `visit_doctor` modifies the `User` + struct, so it must take `&mut self`. +- **Lifetime Elision:** The return type `HealthReport<'_>` indicates that the + output lifetime is tied to the input lifetime of `self`. Explicitly, this + would be `fn visit_doctor<'a>(&'a mut self, ...) -> HealthReport<'a>`. +- **Option combinators:** We use `self.last_blood_pressure.map(...)` to + convenienty calculate the blood pressure change if the previous measurement + exists. + +
+ +- Explain that `HealthReport` borrows from `User`. While `report` exists, `User` + is borrowed (mutably, because it came from `visit_doctor`), so we cannot use + `User` for anything else until `report` is dropped. +- Note the cast to `i32` for blood pressure calculation to allow for negative + changes. + +
From 7b92f2026a64eb50b76a8e691dccbe8870e8277d Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 14 Feb 2026 10:39:45 +0100 Subject: [PATCH 2/2] Refine solution commentary for experienced programmers - Remove redundant or overly simplistic explanations. - Focus on Rust-specific idioms and design choices. - Clean up formatting and technical depth. --- src/borrowing/solution.md | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/borrowing/solution.md b/src/borrowing/solution.md index 92f766f14e4b..96134a822ad0 100644 --- a/src/borrowing/solution.md +++ b/src/borrowing/solution.md @@ -4,24 +4,28 @@ {{#include ../../third_party/rust-on-exercism/health-statistics.rs:solution}} ``` -- **Lifetimes in Structs:** `HealthReport` has a lifetime parameter `'a` because - it contains a reference `patient_name: &'a str`. This ensures that the report - cannot outlive the `User` it refers to. -- **Mutable Reference (`&mut self`):** `visit_doctor` modifies the `User` - struct, so it must take `&mut self`. -- **Lifetime Elision:** The return type `HealthReport<'_>` indicates that the - output lifetime is tied to the input lifetime of `self`. Explicitly, this - would be `fn visit_doctor<'a>(&'a mut self, ...) -> HealthReport<'a>`. -- **Option combinators:** We use `self.last_blood_pressure.map(...)` to - convenienty calculate the blood pressure change if the previous measurement - exists. +The solution explores how structs can capture references and how lifetimes tie +related data structures together: + +- **Lifetimes in Structs:** `HealthReport` contains a reference + `patient_name: &'a str`, which necessitates a lifetime parameter `'a`. This + guarantees that a report cannot outlive the `User` it was created from. +- **Mutable Borrows:** `visit_doctor` takes `&mut self` to update the user's + stats. Because it returns a `HealthReport` that borrows from `self`, the + report's lifetime is tied to the duration of this borrow. +- **Idiomatic `Option` Handling:** `self.last_blood_pressure.map(...)` is used + to concisely compute the change in blood pressure only when a prior value is + available.
-- Explain that `HealthReport` borrows from `User`. While `report` exists, `User` - is borrowed (mutably, because it came from `visit_doctor`), so we cannot use - `User` for anything else until `report` is dropped. -- Note the cast to `i32` for blood pressure calculation to allow for negative - changes. +- **Lifetime Elision:** The signature + `fn visit_doctor(&mut self, ...) -> HealthReport<'_>` uses anonymous + lifetimes. The compiler expands this to indicate that the returned report + borrows from `self`, effectively making the user inaccessible while the report + is in scope. +- **Borrows and Mutation:** Inside `visit_doctor`, the report must be created + _before_ the user's fields are updated, as creating a reference to `self.name` + borrows `self`. In Rust, you cannot modify a value while it is borrowed.