Trusted answers to developer questions
In Rust, the vector is a fundamental data structure provided by the standard collection library. It serves as a dynamic array that can store values of the same data type.
To get the length of a vector in Rust, the len()
method is used. It returns a number from 0
to the maximum value depending on the content of the vector.
Let's see the following code example to get the length of the vector.
Code example
fn main() {
let mut fleet : Vec<&str> = Vec::new();
fleet.push("Ford");
fleet.push("Bentley");
fleet.push("Jeep");
See AlsoBolsa Única SAS: listado definitivo de candidatos/as, varias categorías (corte 2021) - AndaluciaOrienta.netLa bolsa de empleo del SAS abre los plazos de autobaremo del corte 2022-2023SATSE informa sobre la actualización semestral de centros y gestión de disponibilidad en la Bolsa de Empleo del SASGestión de disponibilidades en la BolSASprintln!("{:?}", fleet);
println!("Number Of Cars : {}", fleet.len());
}
Code explanation
Line 2: We use
Vec:new()
to create a vector calledfleet
, which accepts only string values usingVec<&str>
.Lines 3–5: We add cars (values) to the fleet (vector) using
fleet.push("value")
.Line 7: We use
{:?}
to display all the values of thefleet
vector.Line 8:
fleet.len()
display the length of the vector or the number of values in the vector.
RELATED TAGS
rust
CONTRIBUTOR
Olang Daniel
Keep Exploring
Related Courses
TRENDING TOPICS
LEGAL
Cookie Settings
Copyright ©2024 Educative, Inc. All rights reserved.
Did you find this helpful?