Pushing Array values to a Vector in Rust
To push array values to a vector in Rust, you can use the Vec::push
method. Here's an example:
fn main() {
let array = [1, 2, 3, 4, 5];
let mut vector: Vec<i32> = Vec::new();
for &value in &array {
vector.push(value);
}
println!("{:?}", vector);
}
In this example, we have an array called array
with some values. We create an empty vector called vector
using Vec::new()
. Then, we iterate over the elements of the array using a for loop. Inside the loop, we use the push
method to add each value to the vector. Finally, we print the vector to see the result.
When you run this code, the output will be [1, 2, 3, 4, 5]
, which is the array values pushed into the vector.