doc: update book 06_01_arrays (#195)

This commit is contained in:
Maor Levy 2024-12-18 11:12:50 +01:00 committed by GitHub
parent ab1eb5d80c
commit 51cad6bdfe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,6 +21,15 @@ let numbers: [u32; 3] = [1, 2, 3];
This creates an array of 3 integers, initialized with the values `1`, `2`, and `3`.\ This creates an array of 3 integers, initialized with the values `1`, `2`, and `3`.\
The type of the array is `[u32; 3]`, which reads as "an array of `u32`s with a length of 3". The type of the array is `[u32; 3]`, which reads as "an array of `u32`s with a length of 3".
If all array elements are the same, you can use a shorter syntax to initialize it:
```rust
// [ <value> ; <number of elements> ]
let numbers: [u32; 3] = [1; 3];
```
`[1; 3]` creates an array of three elements, all equal to `1`.
### Accessing elements ### Accessing elements
You can access elements of an array using square brackets: You can access elements of an array using square brackets: