November 2017
Intermediate to advanced
264 pages
5h 45m
English
Suppose we have a bunch of alien creatures to populate a game level, then we probably want to store their names in a handy list. Rust's array is just what we need:
// from Chapter 4/code/arrays.rs
let aliens = ["Cherfer", "Fynock", "Shirack", "Zuxu"];
println!("{:?}", aliens);
To make an array, separate the different items by commas and surround the whole with [ ] (rectangular brackets). All items must be of the same type. Such an array is of fixed size (it must be known at compile time) and cannot be changed; it is stored in one contiguous piece of memory in stack memory.
If the items have to be modifiable, declare your array with let mut; however even then the number of items cannot change:
let mut aliens = ...
Read now
Unlock full access