Summary
In this chapter, we have learned that, error handling in Rust is explicit: operations that can fail have a two-part return value via the Result
or Option
generic types. You must handle errors in some way, either by unpacking the Result
/Option
values with a match
statement, or by using combinator methods. Unwrapping should be avoided on error types. Instead, use combinators or match expressions to take appropriate action or propagate the error to the caller by using the ?
operator. It is okay to panic when programming errors are so fatal that recovery would be impossible. Panics are mostly non-recoverable, which means that they crash your thread. Their default behavior is unwinding, which can be expensive and can be turned off if programs don't want this overhead. It is advised to be as descriptive as possible when communicating errors, and authors are encouraged to use custom error types in their crates.
In the next chapter, we'll cover some of the advanced aspects of the language...