From de75cc2286763617ab4be2c9cfb3a09f1346f299 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:03:40 +0100 Subject: [PATCH 1/2] 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. --- src/methods-and-traits/solution.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/methods-and-traits/solution.md b/src/methods-and-traits/solution.md index b4a4c92cd998..f86278d42ba9 100644 --- a/src/methods-and-traits/solution.md +++ b/src/methods-and-traits/solution.md @@ -3,3 +3,24 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +- **Trait Implementation:** We implement the `Logger` trait for + `VerbosityFilter`, matching the signature defined in the trait. +- **Composition and Delegation:** The `VerbosityFilter` struct wraps another + logger (the `inner` field). In its implementation of `log`, it adds logic (the + verbosity check) and then delegates the actual logging to the inner logger if + appropriate. +- **Wrapper Pattern:** This demonstrates how to extend behavior by wrapping a + type. In this specific case, `VerbosityFilter` is hardcoded to wrap + `StderrLogger`, but in real-world Rust code, `inner` would typically be + generic over any type that implements `Logger`. + +
+ +- Note that `self.inner.log(...)` works because `StderrLogger` also implements + `Logger`. +- Point out that this wrapper adds functionality (filtering) without modifying + the inner logger's code. This is a key benefit of using traits and + composition. + +
From 27b2d19072304abe3caecc5bf6edacba72228206 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/methods-and-traits/solution.md | 31 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/methods-and-traits/solution.md b/src/methods-and-traits/solution.md index f86278d42ba9..ee8527cb2ad9 100644 --- a/src/methods-and-traits/solution.md +++ b/src/methods-and-traits/solution.md @@ -4,23 +4,24 @@ {{#include exercise.rs:solution}} ``` -- **Trait Implementation:** We implement the `Logger` trait for - `VerbosityFilter`, matching the signature defined in the trait. -- **Composition and Delegation:** The `VerbosityFilter` struct wraps another - logger (the `inner` field). In its implementation of `log`, it adds logic (the - verbosity check) and then delegates the actual logging to the inner logger if - appropriate. -- **Wrapper Pattern:** This demonstrates how to extend behavior by wrapping a - type. In this specific case, `VerbosityFilter` is hardcoded to wrap - `StderrLogger`, but in real-world Rust code, `inner` would typically be - generic over any type that implements `Logger`. +The solution demonstrates how traits enable polymorphic behavior through +composition: + +- **Trait Implementation:** `VerbosityFilter` implements the `Logger` trait, + allowing it to be used anywhere a `Logger` is expected. +- **Wrapper Pattern:** The `VerbosityFilter` struct wraps an instance of + `StderrLogger`. It intercepts the `log` call to apply filtering logic before + delegating to the inner logger. +- **Composition over Inheritance:** Behavior is extended by wrapping one type in + another, rather than through a class hierarchy.
-- Note that `self.inner.log(...)` works because `StderrLogger` also implements - `Logger`. -- Point out that this wrapper adds functionality (filtering) without modifying - the inner logger's code. This is a key benefit of using traits and - composition. +- **Extensibility:** This wrapper pattern (often called a decorator or + middleware) allows adding functionality like filtering, buffering, or + timestamping without modifying the underlying logger. +- **Teaser for Generics:** While `VerbosityFilter` is hardcoded to wrap + `StderrLogger`, it could be made generic to wrap _any_ type that implements + `Logger`. This is covered in the next section.