24 days of Rust - error_chain
If you have a background in Python, Java or C++, you're probably used to
raising exceptions if something goes wrong. Rust doesn't have exceptions.
The official Rust book has a
comprehensive chapter on error handling,
but the TL;DR is we should probably use the Result
type. We can match
on its variants to handle both the happy path and error cases in a very
explicit, if not verbose, way. To address the verbosity, there was a try!
macro that cut down on a lot of pattern matching boilerplate. And as of now
we have an even simpler syntax - the ?
operator. But when there are many
error types, possibly coming from different libraries, making them compose well
still requires a lot of code: From
and Error
implementations and such.
The error_chain
crate was created
to avoid all that remaining boilerplate. Let's see it in action!