How it works...
Although any type will work just fine in a Result arm, Rust offers an error trait that can be implemented for better integration into other crates. An example of that is the actix_web framework's error handling (https://actix.rs/docs/errors/) that works with std::error::Error as well as with its own types (we will look at that in more depth in Chapter 8, Safe Programming for the Web).
In addition to that, the Error trait also offers nesting, and, using dynamic dispatch, all Errors can follow a common API. In step 2, we declare the type and derive the (mandatory) Debug trait. In step 3 and step 4, the remaining implementations follow. The rest of the recipe executes the code.
In this short and sweet recipe, we can create custom ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access