Arrays
An array is similar to a tuple, but an array's values must all be of the same type. It's defined using square brackets []
and separates its values using commas.
Unlike a tuple, an array can be iterated over through indexing.
fn syntax() {
let array = [1, 2, 3, 4, 5];
let mut counter = 0;
let mut total = 0;
while counter < 5 {
total += array[counter];
counter += 1;
}
}
Arrays are allocated on the stack and thus the size of an array is considered to be static
. What this means is that once an array is declared to have a size of n
it cannot be changed to contain more, or fewer, elements than n
.