From 0dad9dcf9ec7284283ef97dd0f192060a70cfad5 Mon Sep 17 00:00:00 2001 From: Bjorn Tipling Date: Sun, 15 Jan 2017 12:27:41 -0800 Subject: [PATCH] An update to patterns documentation As it is written it creates a lot of confusion. --- src/doc/book/patterns.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/doc/book/patterns.md b/src/doc/book/patterns.md index b50fa01b8e2be..24f71608a5bf3 100644 --- a/src/doc/book/patterns.md +++ b/src/doc/book/patterns.md @@ -23,6 +23,33 @@ match x { This prints `one`. +It's possible to create a binding for the value in the any case: + +```rust +let x = 1; + +match x { + y => println!("x: {} y: {}", x, y), +} +``` + +This prints: + +```text +x: 1 y: 1 +``` + +Note it is an error to have both a catch-all `_` and a catch-all binding in the same match block: + +```rust +let x = 1; + +match x { + y => println!("x: {} y: {}", x, y), + _ => println!("anything"), // this causes an error as it is unreachable +} +``` + There’s one pitfall with patterns: like anything that introduces a new binding, they introduce shadowing. For example: