0

In order to create an array in rust it needs to have a predefined size:

arr = [u32; 4];

If instead of 4 I use a variable, it gives an error, unless it is a constant defined for example as:

const SIZE: usize = 4;

The question: is there a way to define the size of the array according to input from the terminal (command line arguments) when running the program? My assumption is that the const has to be defined in compile time, so not using a const. Maybe using a slice this is possible?

Miguel
  • 1,295
  • 12
  • 25
  • 1
    Possible duplicate of [How to set a Rust array length dynamically?](https://stackoverflow.com/questions/34684261/how-to-set-a-rust-array-length-dynamically) – Andra Oct 28 '19 at 01:03

1 Answers1

0

Arrays must have a size known at compile time. If you want a dynamically sized array, try using a Vec instead, which can have an arbitrary size and can also be grown/shrunk as necessary.

apetranzilla
  • 5,331
  • 27
  • 34