How to get the length of a Vector in Rust
In this tutorial, we are going to learn about how to get the length of a Vector in Rust language.
The length of a vector means the total number of elements present in a given vector.
In Rust, vectors are used to store the data of same datatype.
Getting the vector length
To get the length of a vector, we can use the built in len()
method in Rust.
Here is an example, that gets the length of a vector stored in the price
variable.
let mut price = vec![4, 5, 6, 7];
println!("Length is: {}", price.len())
Output:
Length is 4