To create an empty vector in Rust, you can use the Vec::new
method or the vec![]
macro.
In this example,
Vec::new
method to create an empty vector.Vec::new
method returns a new vector that is empty and does not allocate memory until elements are added to it.empty_vector
using the let
keyword.empty_vector
using the println!
macro to verify that it is indeed empty.fn main() {
let empty_vector: Vec<i32> = Vec::new();
println!("Empty vector: {:?}", empty_vector);
}
Empty vector: []
In this example,
vec![]
macro to create an empty vector.vec![]
macro creates a new vector that is empty and can hold any type of elements specified in the assignment.empty_vector
using the let
keyword.empty_vector
using the println!
macro to verify that it is indeed empty.fn main() {
let empty_vector: Vec<i32> = vec![];
println!("Empty vector: {:?}", empty_vector);
}
Empty vector: []
In this tutorial, we learned How to create an Empty Vector in Rust language with well detailed examples.