Skip to content

Commit

Permalink
Remove generics from logger exercise (#1899)
Browse files Browse the repository at this point in the history
The logger exercise comes before the section on generics, and the
purpose of the exercise is for students to get practice writing a trait
implementation, so using generics in the solution is a source of
confusion for students. I've removed the generic and made
`VerbosityFilter` directly hold a `StderrLogger`.
  • Loading branch information
randomPoison authored Mar 11, 2024
1 parent 9059a1a commit 025fbff
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/methods-and-traits/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
minutes: 20
---

# Exercise: Generic Logger
# Exercise: Logger Trait

Let's design a simple logging utility, using a trait `Logger` with a `log`
method. Code which might log its progress can then take an `&impl Logger`. In
Expand Down
6 changes: 3 additions & 3 deletions src/methods-and-traits/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ fn do_things(logger: &impl Logger) {
// ANCHOR_END: setup

/// Only log messages up to the given verbosity level.
struct VerbosityFilter<L: Logger> {
struct VerbosityFilter {
max_verbosity: u8,
inner: L,
inner: StderrLogger,
}

impl<L: Logger> Logger for VerbosityFilter<L> {
impl Logger for VerbosityFilter {
fn log(&self, verbosity: u8, message: impl Display) {
if verbosity <= self.max_verbosity {
self.inner.log(verbosity, message);
Expand Down

0 comments on commit 025fbff

Please sign in to comment.