In Rust, you can check if a vector (dynamic array) is empty by using its is_empty
method.
In this example,
numbers
with some elements.is_empty
method and store the result in the variable is_empty
.fn main() {
let numbers = vec![1, 2, 3];
let is_empty = numbers.is_empty();
println!("Vector is empty: {:?}", is_empty);
}
Vector is empty: false
In this example,
empty_vector
.is_empty
method and store the result in the variable is_empty
.fn main() {
let empty_vector: Vec<i32> = Vec::new();
let is_empty = empty_vector.is_empty();
println!("Vector is empty: {:?}", is_empty);
}
Vector is empty: true
In this tutorial, we learned How to check if an Array is Empty in Rust language with well detailed examples.