From c3e3dc602090f1afbfa9a3e052b6f7f91a7bf3e0 Mon Sep 17 00:00:00 2001 From: gendx Date: Mon, 6 Feb 2023 16:38:01 +0000 Subject: [PATCH] Add speaker notes for pattern-matching/destructuring-arrays.md (#346) --- src/pattern-matching/destructuring-arrays.md | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/pattern-matching/destructuring-arrays.md b/src/pattern-matching/destructuring-arrays.md index 1d268115..d4b1ff13 100644 --- a/src/pattern-matching/destructuring-arrays.md +++ b/src/pattern-matching/destructuring-arrays.md @@ -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}} ``` + +
+ +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"), + } +} +``` + +