1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-24 16:42:36 +02:00

Add speaker notes for pattern-matching/destructuring-arrays.md (#346)

This commit is contained in:
gendx 2023-02-06 16:38:01 +00:00 committed by GitHub
parent 413098cdac
commit c3e3dc6020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,3 +5,26 @@ You can destructure arrays, tuples, and slices by matching on their elements:
```rust,editable
{{#include ../../third_party/rust-by-example/destructuring-arrays.rs}}
```
<details>
Destructuring of slices of unknown length also works with patterns of fixed length.
```rust,editable
fn main() {
inspect(&[0, -2, 3]);
inspect(&[0, -2, 3, 4]);
}
#[rustfmt::skip]
fn inspect(slice: &[i32]) {
println!("Tell me about {slice:?}");
match slice {
&[0, y, z] => println!("First is 0, y = {y}, and z = {z}"),
&[1, ..] => println!("First is 1 and the rest were ignored"),
_ => println!("All elements were ignored"),
}
}
```
</details>