An array is a sequence of values, of the same type. A array is defined with square brackets [].

rust
// Create an array with 2 elements.
let stuff_and_things = ["stuff", "things"];

// Annotate the types of the arrays values and specify the array size.
let stuff_and_things: [&str; 2] = ["stuff", "things"];

// Array elements are accessed with square `[]` brackets 
// and their `index` number.
println!("{}", stuff_and_things[0]); // => stuff
println!("{}", stuff_and_things[1]); // => things

// All the elements of an array can be destructured into a pattern.
let [a, b] = stuff_and_things;

Array Considerations

  • Arrays live on the stack by default and have a fixed size.
  • Traits are only implemented on an array with a size of 32 or less.
  • Arrays with a size greater than 32 lose most of their functionality.

Tags

rust