January 2018
Beginner to intermediate
454 pages
10h 8m
English
An array is a fixed-size collection of elements of the same type. We declare them with square brackets:
let array = [1, 2, 3, 4]; let array: [i16; 4] = [1, 2, 3, 4];
The second line shows how to specify the type of an array. An alternative way to do that is to use a literal suffix:
let array = [1u8, 2, 3, 4];
A literal suffix is the composition of a literal (that is, a constant) and a type suffix, so with the 1 constant and the u8 type, we get 1u8. Literal suffixes can only be used on numbers. This declares an array of 4 elements of the u8 type. Array indexing starts at 0 and bounds checking is done at runtime. Bounds checking is used to prevent accessing memory that is out of bounds, for instance, trying to access the element after ...
Read now
Unlock full access