1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-20 14:31:15 +02:00

Revert "Exercise: method and traits: change output" ()

Reverts google/comprehensive-rust#2383

Since  is merged, to align the goal in , rollback this temp
workaround.
This commit is contained in:
Alex Lai 2025-01-15 18:22:48 +08:00 committed by GitHub
parent 1c709efac0
commit aa548f4431
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 6 deletions
src/methods-and-traits

@ -9,7 +9,7 @@ method. Code which might log its progress can then take an `&impl Logger`. In
testing, this might put messages in the test logfile, while in a production testing, this might put messages in the test logfile, while in a production
build it would send messages to a log server. build it would send messages to a log server.
However, the `StdoutLogger` given below logs all messages, regardless of However, the `StderrLogger` given below logs all messages, regardless of
verbosity. Your task is to write a `VerbosityFilter` type that will ignore verbosity. Your task is to write a `VerbosityFilter` type that will ignore
messages above a maximum verbosity. messages above a maximum verbosity.

@ -19,11 +19,11 @@ pub trait Logger {
fn log(&self, verbosity: u8, message: &str); fn log(&self, verbosity: u8, message: &str);
} }
struct StdoutLogger; struct StderrLogger;
impl Logger for StdoutLogger { impl Logger for StderrLogger {
fn log(&self, verbosity: u8, message: &str) { fn log(&self, verbosity: u8, message: &str) {
println!("verbosity={verbosity}: {message}"); eprintln!("verbosity={verbosity}: {message}");
} }
} }
// ANCHOR_END: setup // ANCHOR_END: setup
@ -31,7 +31,7 @@ impl Logger for StdoutLogger {
/// Only log messages up to the given verbosity level. /// Only log messages up to the given verbosity level.
struct VerbosityFilter { struct VerbosityFilter {
max_verbosity: u8, max_verbosity: u8,
inner: StdoutLogger, inner: StderrLogger,
} }
impl Logger for VerbosityFilter { impl Logger for VerbosityFilter {
@ -44,7 +44,7 @@ impl Logger for VerbosityFilter {
// ANCHOR: main // ANCHOR: main
fn main() { fn main() {
let logger = VerbosityFilter { max_verbosity: 3, inner: StdoutLogger }; let logger = VerbosityFilter { max_verbosity: 3, inner: StderrLogger };
logger.log(5, "FYI"); logger.log(5, "FYI");
logger.log(2, "Uhoh"); logger.log(2, "Uhoh");
} }