pub fn discard_while<T>(
    iter: &mut impl Iterator<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.

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 debug assertions are enabled, a panic is guaranteed.

Panics

This function might panic if the iterator has more than usize::MAX non-matching elements.

Example

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 mut range = 1..=10;
let result = discard_while(&mut range, |&n| true);
assert_eq!(result, (None, 10));
assert!(range.is_empty());

If the first element that is encountered does not fulfill the condition, 0 is returned as the second return value.

let mut range = 1..=10;
let result = discard_while(&mut range, |&n| false);
assert_eq!(result, (Some(1), 0));
assert_eq!(range, 2..=10);