std-method

Vec::dedup_by

Remove later consecutive elements when the comparison closure returns true. The closure receives mutable references in reverse order relative to the vector. Expected stdout (not executed): [10, 22] Common mistake: Assuming the first closure argument is the earlier element.

Formal Syntax & Structure

rust syntaxrust
vector.dedup_by(|later, earlier| same_group)

Working Example

Example Snippetrust
fn main() {    let mut readings = vec![10, 11, 22, 23];    readings.dedup_by(|later, earlier| *later / 10 == *earlier / 10);    println!("{readings:?}");}
Rust Reference