pub fn discard_while<T>(
iter: impl IntoIterator<Item = T>,
cond: impl FnMut(&T) -> bool,
) -> (Option<T>, usize)Expand description
Advance an iterator as long as a condition on the yielded items holds. Returns the first item that no longer satisfies the condition, if any, and the number of items discarded.
This is similar to a combination of find and position.
Note: This function takes any implementation of IntoIterator, which includes
iterators themselves and mutable references to them.
§Overflow Behavior
The method does no guarding against overflows, so if there are more than usize::MAX
non-matching elements, it either produces the wrong result or panics.
If overflow checks are enabled, a panic is guaranteed.
§Panics
This function might panic if the iterator has more than usize::MAX non-matching elements.
§Examples
Basic usage:
let mut range = 1..=10;
let result = discard_while(&mut range, |&n| n != 5);
assert_eq!(result, (Some(5), 4));
assert_eq!(range, 6..=10);If the iterator ends before an item that does not fulfill the condition
is encountered, None is returned as the first return value.
let arr = [6, 5, 4, 3, 2, 1];
let result = discard_while(arr, |&n| true);
assert_eq!(result, (None, 6));If the first element that is encountered does not fulfill the condition,
0 is returned as the second return value.
let mut range = 2..=8;
let result = discard_while(&mut range, |&n| false);
assert_eq!(result, (Some(2), 0));
assert_eq!(range, 3..=8);