std-method
Vec::dedup_by_key
Remove consecutive elements with equal extracted keys, keeping the first element of each run. Sort by the same key first if grouping all equal keys is desired. Expected stdout (not executed): [(1, "first"), (2, "third"), (1, "last")] Common mistake: Expecting nonadjacent duplicates to disappear without grouping.
Formal Syntax & Structure
rust syntaxrust
vector.dedup_by_key(|item| key)Working Example
Example Snippetrust
fn main() { let mut rows = [(1, "first"), (1, "second"), (2, "third"), (1, "last")]; rows.dedup_by_key(|row| row.0); println!("{rows:?}");}