About Contact me Presentations rss feed  rss

Rust, array type not generic over the size of an array type.

04 Jan 2019


You can declare and initialise a mutable array in Rust like this

let mut xs : [u32; 8] = [1,2,3,4,5,6,7,8];

alternatively you could separate the initialisation like:

let mut xs : [u32; 8] ;
xs = [1,2,3,4,5,6,7,8];

I was surprised when I was not able to re-assign the array with an array of different size… when doing this:

let mut xs : [u32; 8] = [1,2,3,4,5,6,7,8];
xs = [2,4];

you get this error

error[E0308]: mismatched types                                               
  --> src/                                                      
34 |     xs = [2,4];                                                         
   |          ^^^^^ expected an array with a fixed size of 8 elements, found one with 2 elements
   = note: expected type `[u32; 8]`                                          
              found type `[u32; 2]`                                          
error: aborting due to previous error                                        
For more information about this error, try `rustc --explain E0308`. 

error[E0308]: mismatched types

arrays of differnt sizes are different types. I thought that was really interesting. It is documented

This limitation on the size N exists because Rust does not yet support code that is generic over the size of an array type. [Foo; 3] and [Bar; 3] are instances of same generic type [T; 3], but [Foo; 3] and [Foo; 5] are entirely different types. As a stopgap, trait implementations are statically generated up to size 32. (from source)

(BTW kudos to the Rust people for very nice error messages)

If one really wants to use a set lenght array you can still do

let mut xs : [u32; 8] = [1,2,3,4,5,6,7,8];
xs[0] = 2;
xs[1] = 4;
If you want to discuss this post, the best place right now for me is mastodon, please message me with your comment/question.