Rust / Semi Structured Logs Exercise

I am currently doing the Semi Structured Logs exercise but the test keeps failing with this function

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum LogLevel {
    Info,
    Warning,
    Error,
}
pub fn log(level: LogLevel, message: &str) -> String {
    match level {
        Error => format!("[ERROR]: {}", message),
        Info => format!("[INFO]: {}", message),
        Warning => format!("[WARNING]: {}", message),
        _ => unreachable!()
    }
}

From what I understand, we are meant to use a match statement to log out the respective log level and message but the test keeps failing, Here is a snippet of the failed test cases

thread 'log_emits_warning' panicked at 'assertion failed: `(left == right)`
  left: `"[ERROR]: Timezone not set"`,
 right: `"[WARNING]: Timezone not set"`', tests/semi-structured-logs.rs:32:5```
thread 'log_emits_info' panicked at 'assertion failed: `(left == right)`
  left: `"[ERROR]: Timezone changed"`,
 right: `"[INFO]: Timezone changed"`', tests/semi-structured-logs.rs:23:5```

I don’t know what I could be doing wrong, I’d appreciate any help.

P:S I am new to Rust :)

1 Like

Hi there. I need the full code to help efficiently. The mentoring feature is best to help with issues on a particular exercise, the mentor will automatically be able to see all your code.

I believe it’s not possible to open a mentoring request with failing tests in the online editor. But you can submit a failing exercise with the CLI and request mentoring on that.

I also just recommend working locally because you’ll get a better editor experience, especially if you enable the Rust plugin of your favorite editor.

Here’s the documentation for working locally in general, and here’s the documentation for the Rust track.

I’ll be happy to help out with any issues.

2 Likes

Your types in the match are not exact enough, you’ll need to point to the LogLevel enum itself:

pub fn log(level: LogLevel, message: &str) -> String {
    match level {
        LogLevel::Error => format!("[ERROR]: {}", message),
        LogLevel::Info => format!("[INFO]: {}", message),
        LogLevel::Warning => format!("[WARNING]: {}", message),
        _ => unreachable!()
    }
}
1 Like

I don’t think that is the issue. Does leaving off the LogLevel:: produce the reported error messages for you?

Yes, it does.

Hey guys, thank you very much for the response. I do appreciate it. @radar you are right, the types were not exact enough, I tried your solution and that fixed it. Thank you for pointing that out!

I was confused why it would even build in that case, but I see now that uppercase-initial names are legal.

@iyosayi I recommend using Rust-Analyzer. It warns about this problem.