Arrays
-
Every element of an array must have the same type.
-
Arrays have a fixed length.
Creation
-
Defining values manually:
let a = [1, 2, 3, 4, 5]; let b: [i32; 5] = [1, 2, 3, 4, 5]; // [Type; number_of_elements] -
Creating with identical elements:
let a = [3; 5]; // Same thing. let b = [3, 3, 3, 3, 3];
Access
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
-
Out of Bounds :
-
If the index is out of bounds, Rust will panic.
-
In many low-level languages, this kind of check is not done, and when you provide an incorrect index, invalid memory can be accessed. Rust protects you against this kind of error by immediately exiting instead of allowing the memory access and continuing.
-