Trait opt_reduce::OptionExt [−][src]
pub trait OptionExt { type T; fn reduce<F>(self, other: Option<Self::T>, f: F) -> Option<Self::T>
where
F: FnOnce(Self::T, Self::T) -> Self::T; }
Expand description
Option<_>
extension that adds reduce
method to it.
Associated Types
Required methods
Merges self
with another Option
.
Returns
Some(f(l, r))
if both options areSome(_)
Some(x)
if either of the options isSome(x)
and the other isNone
None
if both options areNone
This is an extension version of reduce
.
Examples
use opt_reduce::OptionExt as _; use std::cmp::min; use std::ops::Add; let x = Some(2); let y = Some(4); assert_eq!(x.reduce(y, Add::add), Some(6)); assert_eq!(x.reduce(y, min), Some(2)); assert_eq!(x.reduce(None, Add::add), x); assert_eq!(None.reduce(y, min), y); assert_eq!(None.reduce(None, i32::add), None);