To get the length of an array in Rust, you can use the len
method.
In this example,
numbers
with some values.len
method to get the length of the array and store the returned value in length
variable.length
to standard output.fn main() {
let numbers = [1, 2, 3, 4, 5];
let length = numbers.len();
println!("Length of array is: {}", length);
}
Length of array is: 5
In this example,
names
with some strings.len
method to get the length of the array and store the returned value in length
variable.length
to standard output.fn main() {
let names = ["Alice", "Bob", "Charlie"];
let length = names.len();
println!("Length of array is: {}", length);
}
Length of array is: 3
In this tutorial, we learned How to Get Array Length in Rust language with well detailed examples.