80

When I tried to add a const array in the global scope using this code:

static NUMBERS: [i32] = [1, 2, 3, 4, 5];

I got the following error:

error: mismatched types:
 expected `[i32]`,
    found `[i32; 5]`
(expected slice,
    found array of 5 elements) [E0308]

static NUMBERS2: [i32] = [1, 2, 3, 4, 5];
                         ^~~~~~~~~~~~~~~

The only way I found to deal with this problem is to specify the length in the type:

static NUMBERS: [i32; 5] = [1, 2, 3, 4, 5];

Is there a better way? It should be possible to create an array without manually counting its elements.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Oleg Eterevsky
  • 1,484
  • 2
  • 12
  • 14
  • 1
    If you are looking for some discussion about **why** Rust was designed this way, see [this forum thread](https://users.rust-lang.org/t/solved-why-cant-the-number-of-elements-of-const-array-be-inferred/8569). – phoenix Dec 29 '16 at 15:50
  • 6
    If you want it to be `const`, best to write like: `const NUMBERS: [i32; 5] = [1, 2, 3, 4, 5];` – phoenix Sep 09 '17 at 17:34

2 Answers2

87

Using [T; N] is the proper way to do it in most cases; that way there is no boxing of values at all. There is another way, though, which is also useful at times, though it is slightly less efficient (due to pointer indirection): &'static [T]. In your case:—

static NUMBERS: &'static [i32] = &[1, 2, 3, 4, 5];
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • 16
    If you're looking for a way to do this with strings: http://stackoverflow.com/a/32383866/1349450 – Michael Yoo Jul 21 '16 at 22:47
  • clippy tells me that "statics have by default a `'static` lifetime" – Roberto Leinardi Dec 05 '20 at 20:14
  • 2
    @RobertoLeinardi: from your comment, I suspect you may be misunderstanding something. What Clippy will be saying there is that if you *elide* lifetimes in statics, it’ll infer `'static`—and that is something new since I first wrote the answer, you couldn’t elide lifetimes in statics back then. But `'static` is the only lifetime possible in statics, since they’re being stored in the read-only memory of the binary and must necessarily live forever. But concerning this question and answer, lifetimes are a bit of a red herring; the key thing is the difference between an array and a slice. – Chris Morgan Dec 08 '20 at 18:54
  • And how can I create a big static matrix? example in c++11 I can write `static constexpr int my_arr[64][64]{ {13,44...},{..}.... }` – gekomad Apr 13 '21 at 09:33
  • 1
    @gekomad: type composition is much saner in Rust: arrays are `[T; N]` and values are `[…]`, so you can make them multidimensional with straightforward nesting, like `static MY_ARR: [[i32; 64]; 64] = [[13, 44, …], […], …];`. (Also, your comment is barely related to the question here, please ask new questions for things like that—or search, it’s probably answered already.) – Chris Morgan Jun 01 '21 at 08:22
9

You can use const for that, here is an example:

const NUMBERS: &'static [i32] = &[1, 2, 3, 4, 5];
  • I prefer this. The difference: [*What is the difference between a constant and a static variable and which should I choose?*](https://stackoverflow.com/questions/52751597/what-is-the-difference-between-a-constant-and-a-static-variable-and-which-should) – ynn Jan 27 '23 at 04:23