rustHow to compare two HashSets in Rust?
Comparing two HashSets in Rust is easy and efficient. The HashSet::is_subset and HashSet::is_superset methods can be used to compare two HashSets.
Example code
let set_a: HashSet<i32> = [1, 2, 3].iter().cloned().collect();
let set_b: HashSet<i32> = [2, 3, 4].iter().cloned().collect();
assert!(set_a.is_subset(&set_b));
assert!(!set_b.is_subset(&set_a));
Output example
assertion successful
assertion successful
Code explanation
let set_a: HashSet<i32> = [1, 2, 3].iter().cloned().collect();- This line creates aHashSetcalledset_awith elements1,2, and3.let set_b: HashSet<i32> = [2, 3, 4].iter().cloned().collect();- This line creates aHashSetcalledset_bwith elements2,3, and4.assert!(set_a.is_subset(&set_b));- This line checks ifset_ais a subset ofset_b.assert!(!set_b.is_subset(&set_a));- This line checks ifset_bis not a subset ofset_a.
Helpful links
Related
More of Rust
- How to replace strings using Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace all matches using Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex to match a double quote in Rust?
- How to perform matrix operations in Rust?
- How to sort a Rust HashMap?
- How to use regex lookahead in Rust?
- How to insert an element into a Rust HashMap if it does not already exist?
- How to use regex lookbehind in Rust?
See more codes...