Like Go, Rust favor errors as values in the case of recoverable errors and panics in the case of unrecoverable errors. Unlike Go, Rust has a rich type system with Algebraic data types, which opens up the possibilities for error handling via a Result<T, E>
type.
We can use Pattern matching to extract either the value or the error from a Result
type. Several methods such as ok
, expect
and unwrap
are defined on this type so we can more idiomatically handle the errors.
The ?
operator can be used to propagate errors early to the caller, much like the if err != nil return err
pattern in Go. This operator can be used on the Result
and Option
types (or any type that implements the FromResidual
trait).
Additionally, the main
function can also return a Result
(any type that implements the std::process::Termination
trait to be more precise). In that case, the program exists with a return code of 0 or non 1 depending of the result value.