Skip to content

Commit 7c38de8

Browse files
committed
Auto merge of #26108 - Marwes:field_pun_docs, r=steveklabnik
Adds a mention for the short form pattern syntax. Now without creating a PR to my own fork! #25779
2 parents bfd70aa + 219ddd1 commit 7c38de8

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
@@ -221,12 +221,27 @@ struct Point {
221221
let origin = Point { x: 0, y: 0 };
222222

223223
match origin {
224-
Point { x: x, y: y } => println!("({},{})", x, y),
224+
Point { x, y } => println!("({},{})", x, y),
225225
}
226226
```
227227

228228
[struct]: structs.html
229229

230+
We can use `:` to give a value a different name.
231+
232+
```rust
233+
struct Point {
234+
x: i32,
235+
y: i32,
236+
}
237+
238+
let origin = Point { x: 0, y: 0 };
239+
240+
match origin {
241+
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
242+
}
243+
```
244+
230245
If we only care about some of the values, we don’t have to give them all names:
231246

232247
```rust
@@ -238,7 +253,7 @@ struct Point {
238253
let origin = Point { x: 0, y: 0 };
239254

240255
match origin {
241-
Point { x: x, .. } => println!("x is {}", x),
256+
Point { x, .. } => println!("x is {}", x),
242257
}
243258
```
244259

@@ -255,7 +270,7 @@ struct Point {
255270
let origin = Point { x: 0, y: 0 };
256271

257272
match origin {
258-
Point { y: y, .. } => println!("y is {}", y),
273+
Point { y, .. } => println!("y is {}", y),
259274
}
260275
```
261276

0 commit comments

Comments
 (0)