Quick Reference

Functions

The functions provided fall into a few main types:

  • Input functions convert values into result types
  • Output functions convert result types back into values
  • Transform functions operate on result types
  • Predicate functions return booleans
  • Collection functions provide an interface to access collections without exceptions.

Input Functions

Works onFunctionDescription
Resultto_resultConvert nullable to Result
Optionto_optionConvert nullable to Option

Output Functions

Works onFunctionDescription
BothunwrapReturn inner value or throw an error
Bothunwrap_orReturn inner value or default value
Bothto_nullableConvert Result/Option into a nullable value

Transform Functions

Works onFunctionDescription
ResultokConvert Result to Option
Optionok_orConvert Option to Result
Bothtry_mapMap a function over one or more result values
Resultmap_errMap a function over an error value
Bothand_thenChain fallable functions together
Bothtry_collectCollect an iterable of results into a result containing an array
Optiontry_collect_optionVersion of try_collect specialized for Option. Unexported.
Resulttry_collect_resultVersion of try_collect specialized for Result. Unexported.
BothflattenFlatten a nested result type

Predicate Functions

Works onFunctionDescription
Resultis_okReturn if a Result is Ok
Resultis_errReturn if a Result is Err
Optionis_someReturn if an Option is Some.
Optionis_noneReturn if an Option is None
Bothhas_valReturn if a result type is a success.

Collection Functions

Works onFunctionDescription
Optiontry_pop!Try to pop! a value from a collection. Also works with Iterators.Stateful.
Optiontry_getTry to get a value from a collection
Optiontry_peekTry to peek a value from an iterator
Optiontry_firstTry to get the first value in a collection
Optiontry_lastTry to get the last value in a collection

Macros

MacroDescription
@unwrap_orShort-circuiting version of unwrap_or, which allows for the embedding of control statements.
@try_unwrapUnwraps a value or bubbles an error upstream.
@some_ifEvaluates and returns Some if a predicate is satisfied
@catch_resultCatch an exception and return it as a Result instead
@if_letConditionally unwrap a value inside of a block
@while_letRun a loop while successful

Operators

The following operators are overloaded for use with Result types:

OperatorDescription
Base.:&And/all operator, returns the first Err value. Supports closures for lazy evaluation.
Base.:|Or/any operator, returns the first Ok value. Supports closures for lazy evaluation.
Base.:!Flips Ok and Error values, turning Result{T, E} into Result{E, T}.

In addition, three new operators are introduced for use with Results and Options:

OperatorDescription
try_map: applies a function to the inside of a result type.
Argument-flipped version of try_map.
and_then/monadic bind: connects fallable functions together and returns errors early.

These new operators are not exported by default.