Arrays and Slices

An array or slice is a range of memory that contains multiple values, which must have the same type. Arrays and slices cannot grow or shrink, their size is fixed. While the length of slices is only known at runtime, the length of arrays is part of their type.

Example
The variable  has the type. Because its length is part of its type, the compiler ensures that it can't be assigned an array with the wrong length.

Slices are dynamically sized. This means that they can only exist behind a reference or in pointer type such as. There are two ways to create a slice:


 * Index an array or slice with a range, e.g.  creates a slice of the whole array, whereas   only creates a slice of the 2nd to 7th element of the array.
 * Put an array behind a reference or in a pointer type and then coerce it into a slice. The coercion is possible if the reference or pointer type implements the trait.

Because slices are dynamically sized, references to slices are fat — they're twice as big as normal references, because they have to store the slice's memory address as well as their length: