about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2015-04-18 15:52:29 -0400
committerSteve Klabnik <steve@steveklabnik.com>2015-04-20 09:37:48 -0400
commit9661efac8bd811abfb032c73c1dcdc90cd761dd8 (patch)
tree3f87f7da354cea6edbf75c9d24558c44dd2cf45b /src/doc
parent836c8a826b6b21dd38bbef9d4988e90a1cc0e852 (diff)
downloadrust-9661efac8bd811abfb032c73c1dcdc90cd761dd8.tar.gz
rust-9661efac8bd811abfb032c73c1dcdc90cd761dd8.zip
TRPL editing: patterns
Partially addresses #24388
Diffstat (limited to 'src/doc')
-rw-r--r--src/doc/trpl/patterns.md40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/doc/trpl/patterns.md b/src/doc/trpl/patterns.md
index c88e3a0f9ed..97a3dfe8a76 100644
--- a/src/doc/trpl/patterns.md
+++ b/src/doc/trpl/patterns.md
@@ -21,6 +21,8 @@ match x {
 }
 ```
 
+This prints `one`.
+
 # Multiple patterns
 
 You can match multiple patterns with `|`:
@@ -35,6 +37,8 @@ match x {
 }
 ```
 
+This prints `one or two`.
+
 # Ranges
 
 You can match a range of values with `...`:
@@ -48,7 +52,21 @@ match x {
 }
 ```
 
-Ranges are mostly used with integers and single characters.
+This prints `one through five`.
+
+Ranges are mostly used with integers and `char`s:
+
+```rust
+let x = '💅';
+
+match x {
+    'a' ... 'j' => println!("early letter"),
+    'k' ... 'z' => println!("late letter"),
+    _ => println!("something else"),
+}
+```
+
+This prints `something else`
 
 # Bindings
 
@@ -64,6 +82,8 @@ match x {
 }
 ```
 
+This prints `got a range element 1`.
+
 # Ignoring variants
 
 If you’re matching on an enum which has variants, you can use `..` to
@@ -83,6 +103,8 @@ match x {
 }
 ```
 
+This prints `Got an int!`.
+
 # Guards
 
 You can introduce ‘match guards’ with `if`:
@@ -102,6 +124,8 @@ match x {
 }
 ```
 
+This prints `Got an int!`
+
 # ref and ref mut
 
 If you want to get a [reference][ref], use the `ref` keyword:
@@ -114,6 +138,8 @@ match x {
 }
 ```
 
+This prints `Got a reference to 5`.
+
 [ref]: references-and-borrowing.html
 
 Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
@@ -130,7 +156,7 @@ match x {
 
 # Destructuring
 
-If you have a compound data type, like a `struct`, you can destructure it
+If you have a compound data type, like a [`struct`][struct], you can destructure it
 inside of a pattern:
 
 ```rust
@@ -146,6 +172,8 @@ match origin {
 }
 ```
 
+[struct]: structs.html
+
 If we only care about some of the values, we don’t have to give them all names:
 
 ```rust
@@ -161,6 +189,8 @@ match origin {
 }
 ```
 
+This prints `x is 0`.
+
 You can do this kind of match on any member, not just the first:
 
 ```rust
@@ -176,6 +206,8 @@ match origin {
 }
 ```
 
+This prints `y is 0`.
+
 This ‘destructuring’ behavior works on any compound data type, like
 [tuples][tuples] or [enums][enums].
 
@@ -187,10 +219,10 @@ This ‘destructuring’ behavior works on any compound data type, like
 Whew! That’s a lot of different ways to match things, and they can all be
 mixed and matched, depending on what you’re doing:
 
-```{rust,ignore}
+```rust,ignore
 match x {
     Foo { x: Some(ref name), y: None } => ...
 }
 ```
 
-Patterns are very powerful.  Make good use of them.
+Patterns are very powerful. Make good use of them.