Vec Dedup Collapses Adjacent Duplicates
Remove neighbors with dedup on a sorted vector, keep unsorted segments honest with plain dedup, and dedup_by_key on a chosen field.
Code
fn main() {
// dedup 只折叠相邻重复:先排序才能全量去重
let mut tags = vec!["ops", "dev", "ops", "dev", "dev"];
tags.sort();
tags.dedup();
println!("sorted + dedup: {tags:?}");
// 未排序时只处理相邻段
let mut raw = vec![3, 1, 3, 3, 2];
raw.dedup();
println!("adjacent only : {raw:?}");
// dedup_by_key:按键判断相邻重复
let mut events = vec![("push", 1), ("push", 2), ("merge", 3), ("merge", 4)];
events.dedup_by_key(|event| event.0);
println!("by key : {events:?}");
}What the code does
Vec Dedup Collapses Adjacent Duplicates comes down to the function main.
Applying it
Running the file prints labeled results such as sorted + dedup:, adjacent only :, by key : — each operation’s effect is visible in the output.
Statements that matter
tags.sort();
Interactive Tools for This Topic
FAQ
- What does Vec Dedup Collapses Adjacent Duplicates print when run?
- No setup needed: run the file and look for labeled lines like `sorted + dedup:`, `adjacent only :`, `by key :` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.