Result
Edit on GitHubUtilities for working with the Result data type.
The Result
type is a special type of enum that exists to represent the possibility of a success case (with the Ok
variant), or an error case (with the Err
variant). Use a Result
as the return type of a function that may return an error.
Create new Result
values using Ok()
and Err()
:
Values
Result.isOk
Checks if the Result is the Ok
variant.
Result.isErr
Checks if the Result is the Err
variant.
Result.toOption
Converts the Result to an Option. The error is discarded and replaced with None
.
Result.flatMap
If the Result is Ok
, applies the given function to the Ok
value and returns the result. Returns the unmodified Err
otherwise.
Result.flatMapErr
If the Result is an Err
, applies the given function to the Err
value and returns the result. Returns the unmodified Ok
value otherwise.
Result.map
If the Result is Ok(x)
, returns Ok(fn(x))
. Returns the unmodified Err
otherwise.
Result.mapErr
If the Result is Err(x)
, returns Err(fn(x))
. Returns the unmodified Ok
value otherwise.
Result.mapWithDefault
If the Result is Ok(x)
, returns fn(x)
. Returns the provided default otherwise.
Result.mapWithDefaultFn
If the Result is Ok(x)
, returns fnOk(x)
. If the Result is Err(y)
, returns fnErr(y)
.
Result.or
If the first Result is Ok
, returns the first Result. Returns the second Result otherwise.
Result.and
If the first Result is Err
, returns the first Result. Returns the second Result otherwise.
Result.peek
If the Result is Ok(x)
, applies the first function to x
. If the Result is Err(y)
, applies the second function to y
.
Result.peekOk
If the Result is Ok(x)
, applies the function to x
.
Result.peekErr
If the Result is Err(y)
, applies the function to y
.