In Rust, you can use the is_empty()
method to check if a Set is not empty.
In this example,
HashSet
type from the Rust standard library and initialize it with some elements.is_empty()
method on the Set to determine if it contains any elements.is_empty()
method returns false
; otherwise, it returns true
.use std::collections::HashSet;
fn main() {
let mut my_set = HashSet::new(); // Or let mut my_set: HashSet<i32> = HashSet::new();
my_set.insert(1);
if !my_set.is_empty() {
println!("The Set is not empty");
} else {
println!("The Set is empty");
}
}
The Set is not empty
In this example,
HashSet
type.is_empty()
method on the Set to determine if it contains any elements.is_empty()
method returns true
.use std::collections::HashSet;
fn main() {
let empty_set: HashSet<i32> = HashSet::new();
if !empty_set.is_empty() {
println!("The Set is not empty");
} else {
println!("The Set is empty");
}
}
The Set is empty
In this tutorial, we learned How to check if a Set is Not Empty in Rust language with well detailed examples.