To check if a Set contains all the items of another Set in Rust, you can use the HashSet data structure.
In this example,
set1
and set2
.set2
and check if it exists in set1
.set2
is not found in set1
, then set1
does not contain all items of set2
.use std::collections::HashSet;
fn main() {
// Define Sets with items
let set1: HashSet<_> = [1, 2, 3, 4, 5].iter().cloned().collect();
let set2: HashSet<_> = [2, 3].iter().cloned().collect();
// Check if set1 contains all items of set2
let contains_all = set2.iter().all(|&item| set1.contains(&item));
// Print result
println!("Does set1 contain all items of set2? {}", contains_all);
}
Does set1 contain all items of set2? true
In this example,
set1
and set2
.set2
and check if it exists in set1
.set2
is not found in set1
, then set1
does not contain all items of set2
.use std::collections::HashSet;
fn main() {
// Define Sets with items
let set1: HashSet<_> = [1, 2, 3, 4, 5].iter().cloned().collect();
let set2: HashSet<_> = [2, 6].iter().cloned().collect();
// Check if set1 contains all items of set2
let contains_all = set2.iter().all(|&item| set1.contains(&item));
// Print result
println!("Does set1 contain all items of set2? {}", contains_all);
}
Does set1 contain all items of set2? false
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Rust language with well detailed examples.