1 | initial version |
You can use the extend
method of the Vec
type to add elements from an array into a vector in Rust. Here's an example:
let arr = [1, 2, 3];
let mut vec = Vec::new();
vec.extend(&arr);
println!("{:?}", vec); // Output: [1, 2, 3]
In this example, we first create an array arr
with the values [1, 2, 3]
. We then create an empty vector vec
using the Vec::new()
function. We then use the extend
method to add the elements of the array to the vector.
Note that we pass a reference to the array &arr
to the extend
method. This is because the extend
method accepts an iterable, and arrays in Rust are not directly iterable. However, we can pass a reference to the array, which implements the IntoIterator
trait, to the extend
method to add its elements to the vector.