pub fn step_count_from_checked<T: Step>(
iter: impl IntoIterator,
start: T,
) -> Option<T>Expand description
Consumes the iterator, counting the number of iterations,
starting from a given value.
This uses the Step trait to keep track of the count of iterations.
Returns None if the count exceeded the capcity of the count type (T).
This function always fully consumes the iterator, even if None is returned.
Note: This function takes any implementation of IntoIterator,
which includes iterators themselves.
ยงExamples
Basic usage:
let arr = [1, 2, 3];
let count: Option<u16> = step_count_from_checked(arr, 2);
assert_eq!(count, Some(5));Overflow:
let arr = [(); u8::MAX as usize - 1];
let count: Option<u8> = step_count_from_checked(arr, 2);
assert!(count.is_none());Consumption:
let mut range = -2..=i8::MAX as isize;
let count: Option<i8> = step_count_from_checked(&mut range, -1);
assert!(count.is_none());
assert!(range.is_empty());