Trait grabbag::iter::GroupByIterator
[−]
[src]
pub trait GroupByIterator<E>: Iterator<Item=E> + Sized { fn group_by<GroupFn: FnMut(&E) -> G, G>(self, group: GroupFn) -> GroupBy<Self, GroupFn, E, G> { ... } }
Sequence of iterators containing successive elements of the subject which have the same group according to a group function.
Provided Methods
fn group_by<GroupFn: FnMut(&E) -> G, G>(self, group: GroupFn) -> GroupBy<Self, GroupFn, E, G>
Creates an iterator that yields a succession of (group, sub_iterator)
pairs. Each sub_iterator
yields successive elements of the input iterator that have the same group
. An element's group
is computed using the f
closure.
For example:
let v = vec![7usize, 5, 6, 2, 4, 7, 6, 1, 6, 4, 4, 6, 0, 0, 8, 8, 6, 1, 8, 7]; let is_even = |n: &usize| if *n & 1 == 0 { true } else { false }; for (even, mut ns) in v.into_iter().group_by(is_even) { println!("{}...", if even { "Evens" } else { "Odds" }); for n in ns { println!(" - {}", n); } }