1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-28 03:28:32 +02:00
Files
comprehensive-rust/src/pattern-matching/destructuring-arrays.md

31 lines
708 B
Markdown
Raw Normal View History

2022-12-21 16:36:30 +01:00
# Destructuring Arrays
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>