How it works...
Multiple errors may not seem much of an issue at first, but for a clean, readable architecture, it is necessary to address them somehow. An enum that wraps possible variants has been shown to be the most practical solution, and, by implementing std::error::Error (and the std::fmt::Display requirement), the handling of the new error type should be seamless. In steps 3 to 6, we show an example implementation of the required traits in a reductionist fashion. Step 7 shows how to use the wrapping enum and how to use the Display and Error implementations to help match the variants.
Implementing the Error trait will allow interesting aspects in the future, including recursive nesting. Check the documentation at https://doc.rust-lang.org/std/error/trait.Error.html#method.source ...
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