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
This commit is contained in:
parent
dc72834e2b
commit
219ddd1f61
1 changed files with 18 additions and 3 deletions
|
|
@ -196,12 +196,27 @@ struct Point {
|
|||
let origin = Point { x: 0, y: 0 };
|
||||
|
||||
match origin {
|
||||
Point { x: x, y: y } => println!("({},{})", x, y),
|
||||
Point { x, y } => println!("({},{})", x, y),
|
||||
}
|
||||
```
|
||||
|
||||
[struct]: structs.html
|
||||
|
||||
We can use `:` to give a value a different name.
|
||||
|
||||
```rust
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
let origin = Point { x: 0, y: 0 };
|
||||
|
||||
match origin {
|
||||
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
|
||||
}
|
||||
```
|
||||
|
||||
If we only care about some of the values, we don’t have to give them all names:
|
||||
|
||||
```rust
|
||||
|
|
@ -213,7 +228,7 @@ struct Point {
|
|||
let origin = Point { x: 0, y: 0 };
|
||||
|
||||
match origin {
|
||||
Point { x: x, .. } => println!("x is {}", x),
|
||||
Point { x, .. } => println!("x is {}", x),
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -230,7 +245,7 @@ struct Point {
|
|||
let origin = Point { x: 0, y: 0 };
|
||||
|
||||
match origin {
|
||||
Point { y: y, .. } => println!("y is {}", y),
|
||||
Point { y, .. } => println!("y is {}", y),
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue