0

I don't understand why this compiles with the type value of "typpo" in the Match statement.

I am assuming it is because the variable "typpo" is acting as the "_" catch-all variable.

// This stub file contains items which aren't used yet; feel free to remove this module attribute // to enable stricter warnings. #![allow(unused)] /// various log levels #[derive(Clone, PartialEq, Debug)] pub enum LogLevel { Info, Warning, Error, } /// primary function for emitting logs pub fn log(level: LogLevel, message: &str) -> String { match level { LogLevel::Info => info(&message), LogLevel::Warning => warn(&message), typpo => error(&message), } } pub fn info(message: &str) -> String { format!("[INFO]: {}", &message) } pub fn warn(message: &str) -> String { format!("[WARNING]: {}", &message) } pub fn error(message: &str) -> String { format!("[ERROR]: {}", &message) } 

However, if this is true, then why does this also compile, where we have multiple catch all variables? Why does it not warn of an unused code path at "typpo", since the variable above "typppo" already catches all?

// This stub file contains items which aren't used yet; feel free to remove this module attribute // to enable stricter warnings. #![allow(unused)] /// various log levels #[derive(Clone, PartialEq, Debug)] pub enum LogLevel { Info, Warning, Error, } /// primary function for emitting logs pub fn log(level: LogLevel, message: &str) -> String { match level { LogLevel::Info => info(&message), typppo => warn(&message), typpo => error(&message), } } pub fn info(message: &str) -> String { format!("[INFO]: {}", &message) } pub fn warn(message: &str) -> String { format!("[WARNING]: {}", &message) } pub fn error(message: &str) -> String { format!("[ERROR]: {}", &message) } 
3
  • 1
    It's not a type, but a binding. You can access that variable typpo later. You can read more about it in the book chapter 6 Commented Feb 16, 2022 at 10:25
  • For your second question, is't a misfortune, that there is the #![allow(unused)]. Remove it and compile again. The compiler will give you a very good hint. Commented Feb 16, 2022 at 10:27
  • Ah, got it. I did compile it without the #![allow(unused)] but I did it in a web playground on exercism.org, so it didn't show WARNINGS. That's why I assumed it compiled without warning, but I believe there would have been a warning if I could have seen them. Thank you. Commented Feb 16, 2022 at 11:06

1 Answer 1

6

typpo is indeed interpreted as a catch-all pattern that binds to the entire expression. And in the second example, typpo is indeed unreachable because match arms are evaluated in order.

The compiler does warn about both of these ( String { format!("[WARNING]: {}", &message)}pub fn error(message: &str) -> String { format!("[ERROR]: {}", &message)}" rel="noreferrer">playground link), but #![allow(unused)] suppresses both warnings. unused is the name of a lint group that "detect things being declared but not used, or excess syntax".

Use allow(unused_variable) to only suppress the warning about an unused catch-all binding, and allow(unreachable_patterns) to suppress the warning about an unreachable match arm.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, got it. I did compile it without the #![allow(unused)] but I did it in a web playground on exercism.org, so it didn't show WARNINGS. That's why I assumed it compiled without warning, but I believe there would have been a warning if I could have seen them. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.