rust fill vector with range
To fill a vector with a range of values in Rust, you can use the collect
method on the Iterator
trait. Here's an example:
fn main() {
let my_vector: Vec<i32> = (0..10).collect();
println!("{:?}", my_vector);
}
In this example, (0..10)
represents a range from 0 to 9 (inclusive). The collect
method collects the values from the range into a Vec<i32>
, which is then assigned to the my_vector
variable. Finally, the vector is printed using println!
.
This will output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You can adjust the range to fit your specific needs.