unwrap_or_else? I can imagine that other people may experience similar problems, hence the cheatsheet. You can find examples and more detailed description in the official documentation by clicking on function names.
listToMaybeis akin to calling
IntoIterator. I agree with that, since lists in Haskell, being lazy, more or less correspond to Rust iterators. Also, you can iterate over
Optionin Rust by calling
orat the end have a variant with
or_elseat the end:
or_elseetc. Those variants take a closure for the default value and are lazily evaluated. They are recommended when you have a function call returning default value. In Haskell there is no need for this, since it is a lazy language by default.
unwrapwhich just fails if there is no value
unwrap_orwhich provides a default for that
unwrap_or_elsewhere this default is calculated by a closure
unwrap_defaultwhere default is taken from
Defaulttrait implemented on the type
unwrap_nonewhich fails if the value is not
Noneand returns nothing
expectwhich is the same as
unwrap, but takes a custom error message
expect_nonewhich is the same as
unwrap_nonebut takes a custom error message
as_refwhich converts from
Option<&T>which is very handy for pattern matching.
as_mutplays a similar role for mutable references.
replace, they have no analogs in Haskell that does not have the ownership concept.
Option<&T>) to get
Option<T>from those by cloning or copying the referenced value. There are
clonedmethods for this.
get_or_insert_withmethods which allow to insert a new (possibly computed) value into
Noneor just use the value which was there.
transposemethod in Rust is interesting, since it reminds me of
Traversablein Haskell. It basically transpose two layers:
Eitherin Haskell) and
sequencein Haskell does approximately the same, but in a more generic fashion.
orRust has a method
xor, perhaps just for the completeness. You've probably guessed that it returns
Someif and only if there is only one
Somein its arguments.
maybeToListthat convert between trivial lists (with 0 or 1 elements) and
Maybevalues. Rust doesn't have those, since lists are not that ubiquitous, but see the Update 2 above.
Optionthan Haskell because it has to support references, mutability and ownership. On the other hand Haskell outsources some of the combinators to its generic typeclasses:
Monoidetc. so its combinator library seems thinner.