To create a vector of integers in Rust, you can use the vec! macro or manually initialize a vector with integer elements.
In this example,
vec!
macro followed by square brackets []
to create a vector.vec!
macro generates a vector with the specified integers.int_vector
.let int_vector = vec![1, 2, 3, 4, 5];
println!("{:?}", int_vector);
[1, 2, 3, 4, 5]
In this example,
int_vector
of type Vec
.push
method to add integers one by one to the vector.let mut int_vector: Vec<i32> = Vec::new();
int_vector.push(1);
int_vector.push(2);
int_vector.push(3);
int_vector.push(4);
int_vector.push(5);
println!("{:?}", int_vector);
[1, 2, 3, 4, 5]
In this tutorial, we learned How to create a Vector of Integers in Rust language with well detailed examples.