Error display inconsistent
hanakannzashi opened this issue · 1 comments
hanakannzashi commented
This is my custom error type
struct MyError;
impl Debug for MyError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f, "Debug alternate")
} else {
write!(f, "Debug")
}
}
}
impl Display for MyError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f, "Display alternate")
} else {
write!(f, "Display")
}
}
}
This is my test result
let err = MyError;
let err_anyhow = anyhow!(MyError);
println!("{}", err); // Display
println!("{}", err_anyhow); // Display
println!("{:#}", err); // Display alternate
println!("{:#}", err_anyhow); // Display
println!("{:?}", err); // Debug
println!("{:?}", err_anyhow); // Display
println!("{:#?}", err); // Debug alternate
println!("{:#?}", err_anyhow); // Debug alternate
I am quite confused about this result
dtolnay commented
This is intentional and documented in https://docs.rs/anyhow/1.0.72/anyhow/struct.Error.html#display-representations.