I need to create a huge NxN array. Simple arrays are created on the stack, so no success there. Static arrays have to be const
or unsafe mut
, so that's a no.
I tried Box
ing that array:
const N: usize = 1000000;
let mut boxed: Box<[usize; N]> = Box::new([0; N]);
boxed[1] = 1;
But that overflows the stack anyway, presumably, because it creates a temporary array that is then copied into a Box
.
I tried a Vec
of arrays:
const N: usize = 1000000;
let mut v = Vec::<[usize; N]>::with_capacity(10);
v.push([0; N]);
with the same result. As far as I understand with_capacity
only allocates memory; since Rust has no constructors, I still have to push (i.e.) copy something into that memory.
So, what is the proper way of doing that without going nightly for placement new?