Result type.
Result is the type used for returning and propagating
errors. It is an enum with the variants, Ok(T), representing
success and containing a value, and Err(E), representing error
and containing an error value.
Result whenever errors are expected and
recoverable.
A simple function returning Result might be
defined and used like so:
Results must be used
A common problem with using return values to indicate errors is that it is easy to ignore the return value, thus failing to handle the error.Result is annotated with the #[must_use] attribute,
which will cause the compiler to issue a warning when a Result
value is ignored.
Method overview
In addition to working with pattern matching,Result provides a wide
variety of different methods.
Querying the variant
Theis_ok and is_err methods return true if the Result
is Ok or Err, respectively.
Extracting contained values
These methods extract the contained value in aResult when it
is the Ok variant. If the Result is Err:
expectpanics with a provided felt252 error messageunwrappanics with a generic messageunwrap_orreturns the provided default valueunwrap_or_defaultreturns the default value of the typeT(which must implement theDefaulttrait)unwrap_or_elsereturns the result of evaluating the provided function
Result when it
is the Err variant. If the Result is Ok:
expect_errpanics with a provided felt252 error messageunwrap_errpanics with a generic message
Transforming contained values
These methods transformResult to Option:
oktransformsResultintoOption, mappingOk(v)toSome(v)andErr(e)toNoneerrtransformsResultintoOption, mappingOk(v)toNoneandErr(e)toSome(e)
Ok variant:
maptransformsResultintoResultby applying the provided function to the contained value ofOkand leavingErrvalues unchanged
Err variant:
map_errtransformsResultintoResultby applying the provided function to the contained value ofErrand leavingOkvalues unchanged
Result into a value of a possibly
different type U:
map_orapplies the provided function to the contained value ofOk, or returns the provided default value if theResultisErrmap_or_elseapplies the provided function to the contained value ofOk, or applies the provided default fallback function to the contained value ofErr
Boolean operators
These methods treat theResult as a boolean value, where Ok
acts like true and Err acts like false. There are two
categories of these methods: ones that take a Result as input, and
ones that take a function as input.
The and and or methods take another Result as input, and
produce a Result as output. The and method can produce a
Result value having a different inner type U than
Result. The or method can produce a Result
value having a different error type F than Result.
| method | self | input | output |
|---|---|---|---|
and | Err(e) | (ignored) | Err(e) |
and | Ok(x) | Err(d) | Err(d) |
and | Ok(x) | Ok(y) | Ok(y) |
or | Err(e) | Err(d) | Err(d) |
or | Err(e) | Ok(y) | Ok(y) |
or | Ok(x) | (ignored) | Ok(x) |
and_then and or_else methods take a function as input, and
only evaluate the function when they need to produce a new value. The
and_then method can produce a Result value having a
different inner type U than Result. The or_else method
can produce a Result value having a different error type F
than Result.
| method | self | function input | function result | output |
|---|---|---|---|---|
and_then | Err(e) | (not provided) | (not evaluated) | Err(e) |
and_then | Ok(x) | x | Err(d) | Err(d) |
and_then | Ok(x) | x | Ok(y) | Ok(y) |
or_else | Err(e) | e | Err(d) | Err(d) |
or_else | Err(e) | e | Ok(y) | Ok(y) |
or_else | Ok(x) | (not provided) | (not evaluated) | Ok(x) |
The question mark operator, ?
When writing code that calls many functions that return the Result type,
handling Ok/Err can be tedious. The question mark operator, ?,
hides some of the boilerplate of propagating errors up the call stack.
It replaces this:
Iterating over Result
A Result can be iterated over. This can be helpful if you need an
iterator that is conditionally empty. The iterator will either produce
a single value (when the Result is Ok), or produce no values
(when the Result is Err). For example, into_iter
contains Some(v) if the Result is Ok(v), and None if the
Result is Err.
Enums
| Result | The type used for returning and propagating errors. It is an enum with the variants Ok: T , representing success and containing a value, and Err: E , representing error and containing an… |
Traits
| ResultTrait | — |