From a9e78d802a2099acf1103bfbd59458cfc45270de Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:03:14 +0100 Subject: [PATCH 1/2] memory-management: 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/memory-management/solution.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/memory-management/solution.md b/src/memory-management/solution.md index b4a4c92cd998..c1c2d57a6957 100644 --- a/src/memory-management/solution.md +++ b/src/memory-management/solution.md @@ -3,3 +3,27 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +- **Consuming Builder:** The methods on `PackageBuilder` take `mut self` and + return `Self`. This pattern transfers ownership of the builder into the + method, modifies it, and returns ownership back to the caller. This allows + method chaining (e.g., `.version(...).authors(...)`). +- **`impl Into`:** Arguments like `name` and `version` use + `impl Into`. This allows the caller to pass anything that can be + converted into a `String`, such as a string literal (`&str`) or an owned + `String`. It makes the API flexible. +- **Cloning:** In `as_dependency`, we must `clone()` the name and version. The + method takes `&self` (a shared reference), so we cannot move fields out of the + package. Since `Dependency` owns its strings, we must create new copies. +- **Tuple Struct Wrapper:** `PackageBuilder` is a tuple struct wrapping + `Package`. We access the inner package via `self.0`. This hides the + implementation details of `Package` while it's being built. + +
+ +- Discuss why `as_dependency` requires cloning. If it took `self` (by value), it + could move the fields, but that would consume the `Package`, preventing + further use. +- The `dependencies` vector owns the `Dependency` structs. + +
From 5f7aa2303185b94ebb46f4d5d6f470c9521e7645 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/memory-management/solution.md | 36 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/memory-management/solution.md b/src/memory-management/solution.md index c1c2d57a6957..c12f1d5bd1e9 100644 --- a/src/memory-management/solution.md +++ b/src/memory-management/solution.md @@ -4,26 +4,26 @@ {{#include exercise.rs:solution}} ``` -- **Consuming Builder:** The methods on `PackageBuilder` take `mut self` and - return `Self`. This pattern transfers ownership of the builder into the - method, modifies it, and returns ownership back to the caller. This allows - method chaining (e.g., `.version(...).authors(...)`). -- **`impl Into`:** Arguments like `name` and `version` use - `impl Into`. This allows the caller to pass anything that can be - converted into a `String`, such as a string literal (`&str`) or an owned - `String`. It makes the API flexible. -- **Cloning:** In `as_dependency`, we must `clone()` the name and version. The - method takes `&self` (a shared reference), so we cannot move fields out of the - package. Since `Dependency` owns its strings, we must create new copies. -- **Tuple Struct Wrapper:** `PackageBuilder` is a tuple struct wrapping - `Package`. We access the inner package via `self.0`. This hides the - implementation details of `Package` while it's being built. +The solution demonstrates ownership transfer and flexible API design: + +- **Consuming Builder:** Methods take `mut self` and return `Self`. This pattern + transfers ownership into the method, modifies the state, and returns it, + enabling fluid method chaining. +- **`impl Into`:** This trait bound allows callers to pass either `&str` + or `String`, with `into()` performing the conversion to an owned string only + when necessary. +- **Cloning in `as_dependency`:** Since `as_dependency` takes `&self`, it cannot + move fields out of the `Package`. We must `clone()` the strings to create an + owned `Dependency`.
-- Discuss why `as_dependency` requires cloning. If it took `self` (by value), it - could move the fields, but that would consume the `Package`, preventing - further use. -- The `dependencies` vector owns the `Dependency` structs. +- **Why `mut self`?** A consuming builder is idiomatic for one-off construction. + If we used `&mut self`, the methods would return `&mut Self`, and the final + `.build()` call would still need to consume the builder or clone the entire + state. +- **Inner Representation:** `PackageBuilder` uses a tuple struct to wrap + `Package`, providing a clean interface that hides the inner data structure + during construction.