Skip to content

Commit 095f604

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 095f604

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/memory-management/solution.md

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

0 commit comments

Comments
 (0)