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.
for v in &mut w {orfor v in w.iter_mut()would appear to be the usual way of doing this. See also: stackoverflow.com/q/26185618/1233251let w = { let mut array = ...; ...; array };.for v in &mut w {work ifwis not declated as mutable to begin with?was mutable, thus why I'm asking.