1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-15 10:34:18 +02:00

Be clear that the methods-and-traits exercise does not require generics (#2568)

When teaching the course, I got a little tripped up thinking students
would need to make the `VerbosityFilter` generic over `Logger`. Let's be
clearer that this is not required, and will be described later.

This also updates the generic-types slide to repeat the exercise,
completing that thought.
This commit is contained in:
Dustin J. Mitchell
2025-01-23 03:40:59 -05:00
committed by GitHub
parent 15e46379b1
commit b3c57e4cbf
3 changed files with 39 additions and 30 deletions

View File

@ -1,5 +1,5 @@
---
minutes: 20
minutes: 15
---
# Exercise: Logger Trait
@ -14,13 +14,14 @@ verbosity. Your task is to write a `VerbosityFilter` type that will ignore
messages above a maximum verbosity.
This is a common pattern: a struct wrapping a trait implementation and
implementing that same trait, adding behavior in the process. What other kinds
of wrappers might be useful in a logging utility?
implementing that same trait, adding behavior in the process. In the "Generics"
segment this afternoon, we will see how to make the wrapper generic over the
wrapped type.
```rust,compile_fail
{{#include exercise.rs:setup}}
// TODO: Define and implement `VerbosityFilter`.
// TODO: Implement the `Logger` trait for `VerbosityFilter`.
{{#include exercise.rs:main}}
```

View File

@ -26,13 +26,13 @@ impl Logger for StderrLogger {
eprintln!("verbosity={verbosity}: {message}");
}
}
// ANCHOR_END: setup
/// Only log messages up to the given verbosity level.
struct VerbosityFilter {
max_verbosity: u8,
inner: StderrLogger,
}
// ANCHOR_END: setup
impl Logger for VerbosityFilter {
fn log(&self, verbosity: u8, message: &str) {