Skip to content

Commit a9e78d8

Browse files
committed
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.
1 parent 0867e24 commit a9e78d8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/memory-management/solution.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,27 @@
33
```rust,editable
44
{{#include exercise.rs:solution}}
55
```
6+
7+
- **Consuming Builder:** The methods on `PackageBuilder` take `mut self` and
8+
return `Self`. This pattern transfers ownership of the builder into the
9+
method, modifies it, and returns ownership back to the caller. This allows
10+
method chaining (e.g., `.version(...).authors(...)`).
11+
- **`impl Into<String>`:** Arguments like `name` and `version` use
12+
`impl Into<String>`. This allows the caller to pass anything that can be
13+
converted into a `String`, such as a string literal (`&str`) or an owned
14+
`String`. It makes the API flexible.
15+
- **Cloning:** In `as_dependency`, we must `clone()` the name and version. The
16+
method takes `&self` (a shared reference), so we cannot move fields out of the
17+
package. Since `Dependency` owns its strings, we must create new copies.
18+
- **Tuple Struct Wrapper:** `PackageBuilder` is a tuple struct wrapping
19+
`Package`. We access the inner package via `self.0`. This hides the
20+
implementation details of `Package` while it's being built.
21+
22+
<details>
23+
24+
- Discuss why `as_dependency` requires cloning. If it took `self` (by value), it
25+
could move the fields, but that would consume the `Package`, preventing
26+
further use.
27+
- The `dependencies` vector owns the `Dependency` structs.
28+
29+
</details>

0 commit comments

Comments
 (0)