The Rust docs showcase implementing custom error types with a struct like so:
#[derive(Debug, Clone)]
struct Error;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid first item to double")
}
}
However, for me, it is more natural to use enums to write errors like so
#[derive(Debug, Clone)]
enum Error {
Error1,
Error2
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Error1 => write!(f, "Error 1"),
Error::Error2 => write!(f, "Error 2"),
}
}
}
What method would you suggest is more idiomatic or the preferred method of creating custom errors?