What's the best way to initialize large array?


#1

For Example : I want an array of 100000 elements. But if we use [0; 100000] it will definitely give me stack overflow error because fixed arrays are allocated in stack as far as I know in rust.

So what’s the best way to create an array of 100000 in rust ?


How to initialize array in rust
#2

vec![0; 100000] works.


#3

You can use boxed array.

let x = box [1.1f64; 10000000];

It only works in Nightly at the moment. In stable Rust you can make a vector, then unsafely cast it into a boxed array.


#4

[quote=“anmej, post:3, topic:3458”]
In stable Rust you can make a vector, then unsafely cast it into a boxed array.
[/quote]into_boxed_slice is safe.


#5

Cool. Thanks for your help !!