If I wanted to populate a list of numbers, I can use a vector, and hence the heap, by doing this:
let w = (0..1024).collect::<Vec<_>>();
But, if I wanted to avoid the heap I have to use an array. To use an array, I have to use a loop which means I must have a mutable variable:
let mut w = [0u32; 1024];
for i in 0..1024 {
w[i] = i as u32;
}
Is it possible to populate an array without using mutable variables?
This question has been flagged as a dup. I'm not sure how this can possibly be confused.
"How to populate an array without using mut?" means how do I populate an array without using a mutable variable to do so. Any mut
, not just the array variable itself.
"How do I create and initialize an immutable array?" means how do I create an immutable array.