feat(corelib): OptionTrait boolean operators #6936
Open
+297
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Boolean operators
These methods treat the [
Option
] as a boolean value, where [Some
] acts like [true
] and [None
] acts like [false
]. There are two categories of these methods: ones that take an [Option
] as input, and ones that take a function as input (to be lazily evaluated).The
and
,or
, andxor
methods take another [Option
] as input, and produce an [Option
] as output. Only theand
method can produce an [Option<U>
] value having a different inner typeU
than [Option<T>
].and
None
None
and
Some(x)
None
None
and
Some(x)
Some(y)
Some(y)
or
None
None
None
or
None
Some(y)
Some(y)
or
Some(x)
Some(x)
xor
None
None
None
xor
None
Some(y)
Some(y)
xor
Some(x)
None
Some(x)
xor
Some(x)
Some(y)
None
The
and_then
andor_else
methods take a function as input, andonly evaluate the function when they need to produce a new value. Only
the
and_then
method can produce an [Option<U>
] value having adifferent inner type
U
than [Option<T>
].and_then
None
None
and_then
Some(x)
x
None
None
and_then
Some(x)
x
Some(y)
Some(y)
or_else
None
None
None
or_else
None
Some(y)
Some(y)
or_else
Some(x)
Some(x)