rust vec length
In Rust, the Vec
type represents a dynamically sized, growable array. You can obtain the length of a Vec
by calling its len
method. This method returns the number of elements currently stored in the Vec
. Here's an example:
let my_vec = vec![1, 2, 3, 4, 5];
let length = my_vec.len();
println!("Length of my_vec: {}", length);
In this example, my_vec
is a Vec
containing five elements. The len
method is called on my_vec
to retrieve its length, which is then printed to the console.
Output:
Length of my_vec: 5
You can use the len
method to perform various operations based on the length of a Vec
, such as iterating over its elements or checking if it is empty.