1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-07 01:10:09 +02:00

Change array initialization syntax (#2662)

This commit is contained in:
Nicole L 2025-02-27 12:27:43 -08:00 committed by GitHub
parent 0134c25df9
commit bf4e4e34ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,14 +8,18 @@ minutes: 5
```rust,editable
fn main() {
let mut a: [i8; 10] = [42; 10];
a[5] = 0;
let mut a: [i8; 5] = [5, 4, 3, 2, 1];
a[2] = 0;
println!("a: {a:?}");
}
```
<details>
- Arrays can also be initialized using the shorthand syntax, e.g. `[0; 1024]`.
This can be useful when you want to initialize all elements to the same value,
or if you have a large array that would be hard to initialize manually.
- A value of the array type `[T; N]` holds `N` (a compile-time constant)
elements of the same type `T`. Note that the length of the array is _part of
its type_, which means that `[u8; 3]` and `[u8; 4]` are considered two