Result<E, T> shows an error handling with the variants, Err<E>, representing error and containing an error value, and Ok<T>, representing success and containing a passing value.
Almost of all, you don't need to access the internal value with [0] or [1] to check whether a Result<E, T> is Err<E> or Ok<T>. Instead you can use the functions provided by this module.
ok and err functions construct the Result from value to contain. They have individual type Ok<T> and Err<E>, respectively. So sometimes you need to upcast into Result<E, T>.
optionOk transforms Result<E, T> into Option<T>, mapping Ok<T> to Some<T> and Err<E> to None
optionErr transforms Result<E, T> into Option<E>, mapping Err<E> to Some<E> and Ok<T> to None
resOptToOptRes transposes a Result of an Option into an Option of a Result
This function transforms the contained value of the Ok variant:
map transforms Result<E, T> into Result<E, U> by applying the provided function (t: T) => U to the contained value of Ok and leaving Err values unchanged
product transforms Result<E, T> and Result<E, U> into a new Result<E, [T, U]> and leaving Err values occurred at first
This function transforms the contained value of the Err variant:
mapErr transforms Result<E, T> into Result<F, T> by applying the provided function to the contained value of Err and leaving Ok values unchanged
These functions transform a Result<E, T> into a value of possibly different type U:
mapOr applies the provided function to the contained value of Ok, or returns the provided default value if the Result is Err
mapOrElse applies the provided function to the contained value of Ok, or applies the provided default fallback function to the contained value of Err
This functions transform both of the contained value at same time.
biMap applies the two provided functions to the each contained value
mergeOkErr merges the contained values of Result<T, T> into T
These functions treat the Result 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 (to be lazily evaluated).
The and and or methods take another Result as input, and produce a Result as output. The and method can produce a Result<E, U> value having a different inner type U than Result<E, T>. The or method can produce a Result<F, T> value having a different error type F than Result<E, T>. Note that the order of parameters are reversed because of convenience of partial applying.
function
first parameter
second parameter (as self)
output
and
ignored
err(e)
err(e)
and
err(d)
ok(x)
err(d)
and
ok(y)
ok(x)
ok(y)
or
err(d)
err(e)
err(d)
or
ok(y)
err(e)
ok(y)
or
ignored
ok(x)
ok(x)
The andThen and orElse functions take a function as input, and only evaluate the function when they need to produce a new value. The andThen function can produce a Result<E, T> value having a different inner type U than Result<E, T>. The orElse function can produce a Result<F, T> value having a different error type F than Result<E, T>.
Result<E, T>shows an error handling with the variants,Err<E>, representing error and containing an error value, andOk<T>, representing success and containing a passing value.Functions overview
Almost of all, you don't need to access the internal value with
[0]or[1]to check whether aResult<E, T>isErr<E>orOk<T>. Instead you can use the functions provided by this module.Constructors
okanderrfunctions construct theResultfrom value to contain. They have individual typeOk<T>andErr<E>, respectively. So sometimes you need to upcast intoResult<E, T>.Querying the variant
isOkandisErrfunctions returntrueif theResultisOkorErr, respectively. These provides the type assertion on TypeScript.Extracting contained values
These functions extract the contained value in a
Result<E, T>when it is theOkvariant. If it isErr:unwrapthrows anErrorwith a generic messageunwrapOrreturns the provided the default valueunwrapOrElsereturns the result of evaluating the provided functionAnd this function extracts the contained error value
Ein aResult<E, T>. If it isOk:unwrapErrthrows anErrorwith a generic messageTransforming contained values
These functions transform
ResulttoOption:optionOktransformsResult<E, T>intoOption<T>, mappingOk<T>toSome<T>andErr<E>toNoneoptionErrtransformsResult<E, T>intoOption<E>, mappingErr<E>toSome<E>andOk<T>toNoneresOptToOptRestransposes aResultof anOptioninto anOptionof aResultThis function transforms the contained value of the
Okvariant:maptransformsResult<E, T>intoResult<E, U>by applying the provided function(t: T) => Uto the contained value ofOkand leavingErrvalues unchangedproducttransformsResult<E, T>andResult<E, U>into a newResult<E, [T, U]>and leavingErrvalues occurred at firstThis function transforms the contained value of the
Errvariant:mapErrtransformsResult<E, T>intoResult<F, T>by applying the provided function to the contained value ofErrand leavingOkvalues unchangedThese functions transform a
Result<E, T>into a value of possibly different typeU:mapOrapplies the provided function to the contained value ofOk, or returns the provided default value if theResultisErrmapOrElseapplies the provided function to the contained value ofOk, or applies the provided default fallback function to the contained value ofErrThis functions transform both of the contained value at same time.
biMapapplies the two provided functions to the each contained valuemergeOkErrmerges the contained values ofResult<T, T>intoTBoolean operators
These functions treat the
Resultas a boolean value, whereOkacts liketrueandErracts likefalse. There are two categories of these methods: ones that take aResultas input, and ones that take a function as input (to be lazily evaluated).The
andandormethods take anotherResultas input, and produce aResultas output. Theandmethod can produce aResult<E, U>value having a different inner typeUthanResult<E, T>. Theormethod can produce aResult<F, T>value having a different error typeFthanResult<E, T>. Note that the order of parameters are reversed because of convenience of partial applying.anderr(e)err(e)anderr(d)ok(x)err(d)andok(y)ok(x)ok(y)orerr(d)err(e)err(d)orok(y)err(e)ok(y)orok(x)ok(x)The
andThenandorElsefunctions take a function as input, and only evaluate the function when they need to produce a new value. TheandThenfunction can produce aResult<E, T>value having a different inner typeUthanResult<E, T>. TheorElsefunction can produce aResult<F, T>value having a different error typeFthanResult<E, T>.andThenerr(e)err(e)andThenfok(x)f(x)orElseok(x)ok(x)orElseferr(e)f(e)