When I first started writing Rust code, I went through several iterations of error handling. From that experience, I have these learnings:
Embrace the Result<R, E> type for general purpose error handling. Note that "stack frames" will be lost in this error handling unless you do something about it.
Embrace panic!() (or that family of error handling) for asserting preconditions in your code or really any error that could only happen if the program was incorrect. The beauty of a panic is the you will get a backtrace if the RUST_BACKTRACE environment variable is set.
When you are uncertain, use Result<R, E> , as that can easily be converted into a panic!() by the caller.
The next natural question is what type to use as the E parameter of Result<R, E> .
When designing any error type, I consider these things:
Make sure it is an "owned" type, there are rare exceptions to this rule, but working with non- 'static errors is difficult and should be avoided.
When you have a function A that only triggers an error…