1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-10 00:44:21 +02:00

Remove generics from logger exercise (#1899)

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`.
This commit is contained in:
Nicole L 2024-03-11 13:30:38 -07:00 committed by GitHub
parent 9059a1aa38
commit 025fbffa99
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View File

@ -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

View File

@ -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);