mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-16 07:36:05 +02:00
Change array initialization syntax (#2662)
This commit is contained in:
parent
0134c25df9
commit
bf4e4e34ee
@ -8,14 +8,18 @@ minutes: 5
|
|||||||
|
|
||||||
```rust,editable
|
```rust,editable
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut a: [i8; 10] = [42; 10];
|
let mut a: [i8; 5] = [5, 4, 3, 2, 1];
|
||||||
a[5] = 0;
|
a[2] = 0;
|
||||||
println!("a: {a:?}");
|
println!("a: {a:?}");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<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)
|
- 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
|
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
|
its type_, which means that `[u8; 3]` and `[u8; 4]` are considered two
|
||||||
|
Loading…
x
Reference in New Issue
Block a user