In Rust, you can use the len
method to check if a vector (dynamic array) is not empty.
In this example,
numbers
with some elements.len
method and comparing it with 0, and store the result in the variable notEmpty
.fn main() {
let numbers = vec![1, 2, 3];
let notEmpty = numbers.len() != 0;
println!("Array is not empty: {}", notEmpty);
}
Array is not empty: true
In this example,
emptyVector
.len
method and comparing it with 0, and store the result in the variable notEmpty
.fn main() {
let emptyVector: Vec<i32> = vec![];
let notEmpty = emptyVector.len() != 0;
println!("Array is not empty: {}", notEmpty);
}
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in Rust language with well detailed examples.