To create a vector of empty vectors in Rust, you can initialize a vector and populate it with empty vectors using a for loop.
In this example,
empty_vector_list
.empty_vector_list
now contains the desired number of empty vectors.fn main() {
let mut empty_vector_list: Vec<Vec<i32>> = Vec::new();
let num_empty_vectors = 5;
for _ in 0..num_empty_vectors {
empty_vector_list.push(Vec::new());
}
println!("{:?}", empty_vector_list);
}
[[], [], [], [], []]
In this example,
vec!
macro followed by square brackets []
to create a vector of empty vectors.vec!
macro generates a vector with the specified number of empty vectors.fn main() {
let num_empty_vectors = 5;
let empty_vector_list = vec![Vec::new(); num_empty_vectors];
println!("{:?}", empty_vector_list);
}
[[], [], [], [], []]
In this tutorial, we learned How to create a Vector of Empty Vectors in Rust language with well detailed examples.