Result
Enum
Recoverable errors with Result
Ok
Ok
variant indicates the operation was successful, and inside Ok
is the successfully generated value.
Err
Err
variant means the operation failed, and Err
contains information about how or why the operation failed.
?
Operator
Works with Option
and Result
unwrap
or return Err(From::from(err))
- Catch-all error
Under the hood, the?
operator callsFrom::from
on the error value to convert it to a boxed trait object, aBox<dyn error::Error>
. This boxed trait object is polymorphic, and since all errors implement theerror:Error
trait, we can capture lots of different errors in one “Box” object.
Option
Enum
Some()
None
Handling
expect
expect
method can be used in Option
or Result
to provide user-defined message when in case of panic
unwrap
At this point, a None
/Err(_)
value is a programming error and the program is unable to recover from it.