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

The generic parameter. I.e. A in Option<A>.

Required methods

Merges self with another Option.

Returns

  • Some(f(l, r)) if both options are Some(_)
  • Some(x) if either of the options is Some(x) and the other is None
  • None if both options are None

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);

Implementations on Foreign Types

Implementors