Expand description
Print errors with Display instead of Debug when using ? in main().
Use by returning MainError as the error type from main().
Example:
use main_error::MainError;
fn main() -> Result<(), MainError> {
// This prints
// "Error: invalid digit found in string"
// instead of (if you used `Result<(), Box<dyn Error>>` or similar)
// "ParseIntError { kind: InvalidDigit }".
let number: i32 = "not a number".parse()?;
Ok(())
}For convenience, you can also use the MainResult type.
See below for more details.
§The Issue
Since Rust 1.26, main() function can return a Result<T, E>.
This enables the use of ? for convenient error handling in small programs and quick examples (RFC).
Unfortunately, the error is printed via Debug (hardcoded in the standard library, search for “Error:”),
which gives not very pretty or human-friendly output.
For example, this program:
fn main() -> Result<(), ParseIntError> {
let num: i32 = "not a number".parse()?;
// ...
}will print
Error: ParseIntError { kind: InvalidDigit }§Solution
This crate provides MainError as a drop-in replacement for the error type E in your main’s Result<T, E>.
It prints the error via Display instead of Debug, which yields a nicer error message.
For example, the program above can be changed to
use main_error::MainError;
fn main() -> Result<(), MainError> {
let _: i32 = "not a number".parse()?;
// ...
}and now prints:
Error: invalid digit found in string§Details and Drawbacks
MainErrorstores the original error asBox<dyn Error>. This incurs one allocation (on conversion) and one virtual call (on printing). Since there can be exactly one error like this before the program ends, this cost is insignificant.MainErrorimplementsFromfor all types that can be converted into aBox<dyn Error>.- This allows it to be used in place of any type that implements the
Errortrait (see example above). - It can also be used in place of any type that can be converted to a
Box<dyn Error>, e.g.,String.
- This allows it to be used in place of any type that implements the
MainErrordoes not implement theErrortrait itself. Reasons:- It’s not necessary, because the standard library only requires
E: Debugformain() -> Result<T, E>. - You should only be using
MainErrorformain()anyway, whereas theErrortrait is more for interoparability between libraries. - One simply cannot implement
ErrorforMainError, because this would create an overlappingimpl.
Explanation:MainErrorcan be converted from aT: Into<Box<dyn Error>>.Into<Box<dyn Error>>is implemented forE: Erroritself.- If
MainErrorwould implementError, it could be converted from itself. - This collides with the reflexive
impl<T> From<T> for Tin core.
- It’s not necessary, because the standard library only requires
MainErrorimplementsDebugin terms ofDisplayof the underlying error. This is hacky, but unfortunatelyDebugas the output for themainerror case is stable now. The"Error: "part at the beginning of the output comes from the standard library, thus it cannot be changed.
Structs§
- Main
Error - Newtype wrapper around a boxed
std::error::Error.
Type Aliases§
- Main
Result - Convenience type as a shorthand return type for
main().