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
- Regex example to match multiline string in Rust?
- How to replace a capture group using Rust regex?
- How to use regex captures in Rust?
- How to match the end of a line in a Rust regex?
- How to use binary regex in Rust?
- How to use regex to match a group in Rust?
- How to extend a Rust HashMap?
- How to create a Rust regex from a string?
- How to match digits with regex in Rust?
- How to perform matrix operations in Rust?
See more codes...