This is meant to be for people coming from Haskell to Rust or vice versa who want to quickly find the name of corresponding function on optional values. For example, I keep forgetting the names of Rust combinators. Which one I have to use in a particular situation? Is it
or_else
,
unwrap_or
or
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.
Update 1
: as
/u/jkachmar
points out on Reddit that there is a
note
function in some of alternative Preludes in Haskell (or in
errors
package), which is an analog of
ok_or
in Rust. Added to the cheatsheet.
Update 2:
/u/masklinn
says that Haskell
listToMaybe
is akin to calling
next
on an
Iterator
and
maybeToList
is an
Option
implementing
IntoIterator
. I agree with that, since lists in Haskell, being lazy, more or less correspond to Rust iterators. Also, you can iterate over
Option
in Rust by calling
iter
.
Update 3: fixed several typos and errors spotted by Reddit readers. Thank you /u/jroller , /u/jlombera , /u/gabedamien , /u/george____t
Update 4:
use
flatten
from Iterator to implement
catMaybe
, thanks to
/u/MysteryManEusine
.
Cheatsheet
Haskell |
Rust |
Purpose |
Type (Haskell style) |
Maybe |
Option |
type name |
|
Just |
Some |
constructor for value |
|
Nothing |
None |
constructor for no value |
|
isJust |
is_some |
check if has value |
|
isNothing |
is_none |
check if has no value |
|
fmap from Functor |
map |
apply function to value inside |
|
fromJust |
unwrap |
extract a value, fail if there is none |
|
fromMaybe |
unwrap_or |
extract a value or return a given default |
|
(>>=) from Monad |
and_then |
propagate "no value", apply a function to a value, function can return no value too |
|
(<|>) from Alternative |
or |
return first value if present or second if not |
|
(>>) from Monad |
and |
return first value if none or second if not |
|
maybe |
map_or |
takes function and default. Apply function to the value or return default if there is no value |
|
note , maybeToRight (both non- standard) |
ok_or |
transforms optional value to possible error |
|
mapMaybe |
filter_map from Iterator |
applies filter and map simultaneously |
|
catMaybe |
flatten from Iterator |
extracts only values from the list, drops no values |
|
join from Monad |
flatten |
squashes two layers of optionality into one |
|
sequence from Traversable |
transpose |
transposes Option and Result layers (or Either and Maybe in Haskell terms) |
|
Notes
In Rust, all combinators with
or
at the end have a variant with
or_else
at the end:
unwrap_or_else
or
or_else
etc. 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.
In Rust there are many different ways for extracting the optional value:
-
unwrap
which just fails if there is no value -
unwrap_or
which provides a default for that -
unwrap_or_else
where this default is calculated by a closure -
unwrap_default
where default is taken fromDefault
trait implemented on the type -
unwrap_none
which fails if the value is notNone
and returns nothing -
expect
which is the same asunwrap
, but takes a custom error message -
expect_none
which is the same asunwrap_none
but takes a custom error message
Another very useful method in Rust is
as_ref
which converts from
&Option<T>
to
Option<&T>
which is very handy for pattern matching.
as_mut
plays a similar role for mutable references.
In Rust there are several methods related to ownership of the value in the
Option
, like
take
and
replace
, they have no analogs in Haskell that does not have the ownership concept.
In Rust we sometimes want to copy or clone values, it's possible to do so on optional references (
Option<&T>
) to get
Option<T>
from those by cloning or copying the referenced value. There are
copied
and
cloned
methods for this.
It's possible to mutate optional values in Rust. For that we have
get_or_insert
and
get_or_insert_with
methods which allow to insert a new (possibly computed) value into
None
or just use the value which was there.
transpose
method in Rust is interesting, since it reminds me of
sequence
from
Traversable
in Haskell. It basically transpose two layers:
Result
(or
Either
in Haskell) and
Option
.
sequence
in Haskell does approximately the same, but in a more generic fashion.
In addition to
and
and
or
Rust has a method
xor
, perhaps just for the completeness. You've probably guessed that it returns
Some
if and only if there is only one
Some
in its arguments.
Haskell has two functions
listToMaybe
and
maybeToList
that convert between trivial lists (with 0 or 1 elements) and
Maybe
values. Rust doesn't have those, since lists are not that ubiquitous, but see the Update 2 above.
Summary
Rust has more functions to work with
Option
than Haskell because it has to support references, mutability and ownership. On the other hand Haskell outsources some of the combinators to its generic typeclasses:
Semigroup
,
Alternative
,
Monoid
etc. so its combinator library seems thinner.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C程序设计语言
(美)Brian W. Kernighan、(美)Dennis M. Ritchie / 徐宝文、李志译、尤晋元审校 / 机械工业出版社 / 2004-1 / 30.00元
在计算机发展的历史上,没有哪一种程序设计语言像C语言这样应用广泛。本书原著即为C语言的设计者之一Dennis M.Ritchie和著名计算机科学家Brian W.Kernighan合著的一本介绍C语言的权威经典著作。我们现在见到的大量论述C语言程序设计的教材和专著均以此书为蓝本。原著第1版中介绍的C语言成为后来广泛使用的C语言版本——标准C的基础。人们熟知的“hello,World"程序就是由本书......一起来看看 《C程序设计语言》 这本书的介绍吧!