To check if a vector is empty in Rust, you can use the is_empty
method, which returns true
if the vector has no elements.
In this example,
num_vec
.is_empty
method to check if num_vec
is empty.num_vec.is_empty()
returns true
, it means the vector is empty.fn main() {
let num_vec: Vec<i32> = vec![];
if num_vec.is_empty() {
println!("Numeric vector is empty");
} else {
println!("Numeric vector is not empty");
}
}
Numeric vector is empty
In this example,
str_vec
.is_empty
method to check if str_vec
is empty.str_vec.is_empty()
returns true
, it means the vector is empty.fn main() {
let str_vec: Vec<&str> = vec![];
if str_vec.is_empty() {
println!("String vector is empty");
} else {
println!("String vector is not empty");
}
}
String vector is empty
In this tutorial, we learned How to check if a Vector is Empty in Rust language with well detailed examples.