Function opt_reduce::reduce[][src]

pub fn reduce<T, F>(a: Option<T>, b: Option<T>, f: F) -> Option<T> where
    F: FnOnce(T, T) -> T, 
Expand description

Merges 2 options a and b together.

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 a standalone version of OptionExt::reduce.

Examples

use std::cmp::min;
use std::ops::Add;

let x = Some(2);
let y = Some(4);

assert_eq!(opt_reduce::reduce(x, y, Add::add), Some(6));
assert_eq!(opt_reduce::reduce(x, y, min), Some(2));

assert_eq!(opt_reduce::reduce(x, None, Add::add), x);
assert_eq!(opt_reduce::reduce(None, y, min), y);

assert_eq!(opt_reduce::reduce(None, None, i32::add), None);