To check if a Set is empty in Rust, you can use the is_empty method or compare the len to zero.
In this example,
use std::collections::HashSet;
fn main() {
// Define set
let my_set: HashSet<i32> = HashSet::new();
// Check if set is empty using is_empty method
if my_set.is_empty() {
println!("Set is empty");
} else {
println!("Set is not empty");
}
}
Set is empty
In this example,
use std::collections::HashSet;
fn main() {
// Define set
let my_set: HashSet<i32> = HashSet::new();
// Check if set is empty using len method
if my_set.len() == 0 {
println!("Set is empty");
} else {
println!("Set is not empty");
}
}
Set is empty
In this tutorial, we learned How to check if a Set is Empty in Rust language with well detailed examples.