You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-27 19:18:59 +02:00
* Indent code in speaker notes #475 * Update stack.md * Update destructuring-arrays.md * Update hashmap.md * Update traits.md
38 lines
1.0 KiB
Markdown
38 lines
1.0 KiB
Markdown
# 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"),
|
|
}
|
|
}
|
|
```
|
|
|
|
* Create a new pattern using `_` to represent an element.
|
|
* Add more values to the array.
|
|
* Point out that how `..` will expand to account for different number of elements.
|
|
* Show matching against the tail with patterns `[.., b]` and `[a@..,b]`
|
|
|
|
</details>
|