Skip to content

Commit 219ddd1

Browse files
committed
Mention the short form pattern syntax in the book
Explains short form pattern syntax and then introduces the longer pattern matching as a rebinding of the fields instead. #25779
1 parent dc72834 commit 219ddd1

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/doc/trpl/patterns.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,27 @@ struct Point {
196196
let origin = Point { x: 0, y: 0 };
197197

198198
match origin {
199-
Point { x: x, y: y } => println!("({},{})", x, y),
199+
Point { x, y } => println!("({},{})", x, y),
200200
}
201201
```
202202

203203
[struct]: structs.html
204204

205+
We can use `:` to give a value a different name.
206+
207+
```rust
208+
struct Point {
209+
x: i32,
210+
y: i32,
211+
}
212+
213+
let origin = Point { x: 0, y: 0 };
214+
215+
match origin {
216+
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
217+
}
218+
```
219+
205220
If we only care about some of the values, we don’t have to give them all names:
206221

207222
```rust
@@ -213,7 +228,7 @@ struct Point {
213228
let origin = Point { x: 0, y: 0 };
214229

215230
match origin {
216-
Point { x: x, .. } => println!("x is {}", x),
231+
Point { x, .. } => println!("x is {}", x),
217232
}
218233
```
219234

@@ -230,7 +245,7 @@ struct Point {
230245
let origin = Point { x: 0, y: 0 };
231246

232247
match origin {
233-
Point { y: y, .. } => println!("y is {}", y),
248+
Point { y, .. } => println!("y is {}", y),
234249
}
235250
```
236251

0 commit comments

Comments
 (0)