Trusted answers to developer questions
Get Started With Machine Learning
Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.
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.netGestión de disponibilidades en la BolSASBolsa SAS: ¿Cómo subir los méritos a la VEC? - INAFELa bolsa de empleo del SAS abre los plazos de autobaremo del corte 2022-2023println!("{:?}", 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
TRENDING TOPICS
LEGAL
Cookie Settings
Copyright ©2024 Educative, Inc. All rights reserved.
Did you find this helpful?