Logo

Programming-Idioms

History of Idiom 218 > diff from v10 to v11

Edit summary for version 11 by CeaselessBanana:
New Rust implementation by user [CeaselessBanana]

Version 10

2020-01-19, 12:22:31

Version 11

2020-03-17, 13:45:03

Idiom #218 List intersection

Create list c containing all unique elements that are contained in both lists a and b.
c should not contain any duplicates, even if a and b do.
The order of c doesn't matter.

Idiom #218 List intersection

Create list c containing all unique elements that are contained in both lists a and b.
c should not contain any duplicates, even if a and b do.
The order of c doesn't matter.

Extra Keywords
intersect and conjunction
Extra Keywords
intersect and conjunction
Imports
use std::collections::HashSet;
Code
let unique_a = a.iter().collect::<HashSet<_>>();
let unique_b = b.iter().collect::<HashSet<_>>();

let c = unique_a.intersection(&unique_b).collect<Vec<_>>();
Doc URL
https://doc.rust-lang.org/nightly/std/collections/struct.HashSet.html#method.intersection