Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/methods-and-traits/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@
```rust,editable
{{#include exercise.rs:solution}}
```

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.

<details>

- **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.

</details>