Skip to content

Commit 51ed840

Browse files
committed
methods-and-traits: 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 51ed840

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/methods-and-traits/solution.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,23 @@
33
```rust,editable
44
{{#include exercise.rs:solution}}
55
```
6+
7+
- **Trait Implementation:** We implement the `Logger` trait for `VerbosityFilter`,
8+
matching the signature defined in the trait.
9+
- **Composition and Delegation:** The `VerbosityFilter` struct wraps another
10+
logger (the `inner` field). In its implementation of `log`, it adds logic
11+
(the verbosity check) and then delegates the actual logging to the inner logger
12+
if appropriate.
13+
- **Wrapper Pattern:** This demonstrates how to extend behavior by wrapping a
14+
type. In this specific case, `VerbosityFilter` is hardcoded to wrap
15+
`StderrLogger`, but in real-world Rust code, `inner` would typically be generic
16+
over any type that implements `Logger`.
17+
18+
<details>
19+
20+
- Note that `self.inner.log(...)` works because `StderrLogger` also implements
21+
`Logger`.
22+
- Point out that this wrapper adds functionality (filtering) without modifying
23+
the inner logger's code. This is a key benefit of using traits and composition.
24+
25+
</details>

0 commit comments

Comments
 (0)