error[E0308]: mismatched types
--> src/main.rs:2:28
|
2 | const NUMBERS: [i32] = [1, 2, 3, 4, 5];
| ^^^^^^^^^^^^^^^ expected slice, found array of 5 elements
|
= note: expected type `[i32]`
= note: found type `[i32; 5]`
error[E0277]: the trait bound `[i32]: std::marker::Sized` is not satisfied
--> src/main.rs:2:28
|
2 | const NUMBERS: [i32] = [1, 2, 3, 4, 5];
| ^^^^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `[i32]`
|
= note: `[i32]` does not have a constant size known at compile-time
= note: constant expressions must have a statically known size
error: aborting due to 2 previous errors
error: Could not compile `rust-test`.
There are a few times in Rust where we chose the most conservative option, because we weren't comfortable deciding how much convenience is a good idea. const/static declarations are one of them: we don't infer types or let you elide lifetimes at all.
This might be a good change, but it would have to go through all of the other design work for features that we usually do.
Note that you're not using an array here: you're using a slice. [i32] isn't an array, [i32; N] for some N is.
One nice thing about the explicit length is in helping you notice when you have made a breaking change to the public API of your crate. Suppose you have code like:
pub const NUMBERS: [i32] = [1, 2, 3, 4, 5];
Adding an element doesn't feel like a breaking change because the type hasn't changed:
pub const NUMBERS: [i32] = [1, 2, 3, 4, 5, 6];
And yet you have broken downstream code that looks like this:
let _: &[i32; 5] = &johnthagen::NUMBERS;
error[E0308]: mismatched types
--> <anon>:6:24
|
6 | let _: &[i32; 5] = &johnthagen::NUMBERS;
| ^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 5 elements, found one with 6 elements
|
= note: expected type `&[i32; 5]`
= note: found type `&[i32; 6]`
If you want the flexibility of changing the number of elements, use &[i32] as suggested by the StackOverflow answer. If not, the number of elements should be part of the type.
We have the sttatic_lifetimes (hope I recall the name correctly) feature that enables lifetime elision for static and const items. It's likely to hit stable in 1.15.
rustc 1.19.0-nightly (f89d8d184 2017-05-30)
error: expected one of `!`, `(`, `+`, `::`, `;`, `<`, or `]`, found `,`
--> <anon>:1:20
|
1 | const NUMBERS: [i32, _] = [1, 2, 3, 4, 5];
| ^ expected one of 7 possible tokens here
error: aborting due to previous error(s)