pub trait StepCount: Iterator {
// Provided methods
fn step_count<T: Step + Default>(self) -> T
where Self: Sized { ... }
fn step_count_from<T: Step>(self, start: T) -> T
where Self: Sized { ... }
fn step_count_checked<T: Step + Default>(self) -> Option<T>
where Self: Sized { ... }
fn step_count_from_checked<T: Step>(self, start: T) -> Option<T>
where Self: Sized { ... }
}
Expand description
Convenience trait to allow using step_count*
functions as methods.
This trait is implemented for every Iterator
.
Provided Methods§
Sourcefn step_count<T: Step + Default>(self) -> Twhere
Self: Sized,
fn step_count<T: Step + Default>(self) -> Twhere
Self: Sized,
Consumes the iterator, counting the number of iterations.
This uses the Step
trait to keep track of the count of iterations.
The count starts from the default value provided by the Default
trait.
§Panics
Panics if the count exceeds the capacity of the count type (T
).
§Examples
Basic usage:
let arr = [1, 2, 3];
let count: usize = arr.into_iter().step_count();
assert_eq!(count, 3);
Overflow:
let range = 0..u8::MAX as usize + 1;
range.step_count::<u8>();
Sourcefn step_count_from<T: Step>(self, start: T) -> Twhere
Self: Sized,
fn step_count_from<T: Step>(self, start: T) -> Twhere
Self: Sized,
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.
§Panics
Panics if the count exceeds the capacity of the count type (T
).
§Examples
Basic usage:
let arr = [1, 2, 3];
let count: u16 = arr.into_iter().step_count_from(2);
assert_eq!(count, 5);
Overflow:
let range = 0..u8::MAX as usize - 1;
range.step_count_from::<u8>(2);
Sourcefn step_count_checked<T: Step + Default>(self) -> Option<T>where
Self: Sized,
fn step_count_checked<T: Step + Default>(self) -> Option<T>where
Self: Sized,
Consumes the iterator, counting the number of iterations.
This uses the Step
trait to keep track of the count of iterations.
The count starts from the default value provided by the Default
trait.
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.
§Examples
Basic usage:
let arr = [1, 2, 3];
let count: Option<u16> = arr.into_iter().step_count_checked();
assert_eq!(count, Some(3));
Overflow:
let range = 0..u8::MAX as usize + 1;
let count: Option<u8> = range.step_count_checked();
assert!(count.is_none());
Consumption:
let mut range = -1..u8::MAX as isize;
let count: Option<u8> = range.by_ref().step_count_checked();
assert!(count.is_none());
assert!(range.is_empty());
Sourcefn step_count_from_checked<T: Step>(self, start: T) -> Option<T>where
Self: Sized,
fn step_count_from_checked<T: Step>(self, start: T) -> Option<T>where
Self: Sized,
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.
§Examples
Basic usage:
let arr = [1, 2, 3];
let count: Option<u16> = arr.into_iter().step_count_from_checked(2);
assert_eq!(count, Some(5));
Overflow:
let range = 0..u8::MAX as usize - 1;
let count: Option<u8> = range.step_count_from_checked(2);
assert!(count.is_none());
Consumption:
let mut range = -2..i8::MAX as isize;
let count: Option<i8> = range.by_ref().step_count_from_checked(-1);
assert!(count.is_none());
assert!(range.is_empty());