std-method

Vec::dedup

Remove consecutive equal elements, retaining the first of each run. Nonadjacent duplicate values remain unless grouped first. Expected stdout (not executed): [1, 2, 1] [1, 2] Common mistake: Using dedup alone to remove every duplicate from unsorted input.

Formal Syntax & Structure

rust syntaxrust
pub fn dedup(&mut self) where T: PartialEq

Working Example

Example Snippetrust
fn main() {    let mut ids = vec![1, 1, 2, 1];    ids.dedup();    println!("{ids:?}");    ids.sort();    ids.dedup();    println!("{ids:?}");}
Rust Reference