Use the itertools crate's group_by method on an iterator to collect consecutive elements sharing a key into sub-iterators. Add itertools to your Cargo.toml dependencies, import the trait, and chain .group_by() with a closure that extracts the grouping key.
use itertools::Itertools;
fn main() {
let data = vec![1, 2, 2, 3, 3, 3];
let grouped: Vec<_> = data.iter().group_by(|&x| x).into_iter().collect();
println!("{:?}", grouped);
}
Add this to your Cargo.toml:
[dependencies]
itertools = "0.13"