How to initialize array in rust


#1

First of all, Rust language is promising and it’s cool. I’m new to rust andI like to know how to initialize array in rust.

Currently I’m using Rust 1.3.0.

Well I tried to initialize the array using

let a = [0i, …20];

This is not working…


#2

That hasn’t been valid syntax in a long time. As for how to do it now, arrays are covered in the book.


#3

Cool. Thanks for your help !!


#4

you can use

let a = [0; 30]

this will initialize 0 to first 30 elements.


#5

Cool. Do you know, What’s the best way to initialize large array ?

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 ?


#6

For reference, this was cross-posted at: