I want to create a method for my MyError enum (whose variants are the different error types in my program) that returns a String value that describes the given MyError variant. For example:
pub enum MyError {
Error1,
Error2,
}
impl MyError {
pub fn to_str(&self) -> String {
match self {
Error1 => format!("Error1: bla bla bla"),
Error2 => format!("Error2: na na na"),
}
}
}
This is all well and good, but the problem is, I have a new error variant (say Error3) that must pass a parameter to its format!() macro in the method, like this:
Error3 => format!("la la la {:?}", arg),
This parameter can be of any type as long as it can derive the Debug trait. So my solution was
pub enum MyError {
Error1,
Error2,
Error3
}
impl MyError {
pub fn to_str(&self, arg: Option<&impl fmt::Debug>) -> String {
match self {
Error1 => format!("bla bla bla"),
Error2 => format!("na na na"),
Error3 => format!("la la la {:?}", arg),
}
}
}
Where I wrap the trait-bounded parameter in Option since some variants of MyError do not need it (e.g. Error1). This works for the Error3 variant, I can do the following without any compilation error:
eprintln!("{}", MyError::Error3.to_str(Some(vec![1, 2, 3])));
It prints the associated error message of Error3. But when I try to use the method to the other variants which require no additional parameter, e.g. calling
eprintln!("{}", MyError::Error1.to_str(None));
It returns the following compilation error:
type annotations needed
cannot infer type for type parameter `impl fmt::Debug` declared on the associated function `to_str`rustc(E0282)
Why can't the compiler infer the type of None here?