SHARE
TWEET

Untitled

a guest Jan 21st, 2020 83 in 3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// removes and returns values from the input satisfying the condition func_if
  2. ///```
  3. ///let mut input = vec![Some((1, 3)), None, Some((2, 4))];
  4. ///assert_eq!(take_if(&mut input, &|(a, _)| a == 1), vec![Some((1, 3)), None, None]);
  5. ///assert_eq!(input, vec![None, None, Some((2, 4))]);
  6. ///```
  7. pub fn take_if<'a, T>(
  8.    input: &'a mut Vec<Option<T>>,
  9.     func_if: &dyn Fn(&'a T) -> bool,
  10. ) -> Vec<Option<T>> {
  11.    input
  12.        .iter_mut()
  13.        .map(|opt_t| match opt_t.as_ref().map(func_if) {
  14.            Some(true) => opt_t.take(),
  15.            _ => None,
  16.        })
  17.        .collect()
  18. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top