mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
Remove some of the less-commonly-used pattern abilities
Connects to #115. Not fixes, because I'm going to give some of these more motivation before I think we can say we've covered what a new rustacean *needs* to know. Connects to #114 because I removed the trailing off last section, but I do want to give this section a real ending.
This commit is contained in:
parent
f6e536a1a2
commit
cefbd7f16d
@ -1,9 +1,9 @@
|
|||||||
# Patterns
|
# Patterns
|
||||||
|
|
||||||
We've mentioned 'patterns' a few times so far: they're used in `let` bindings,
|
We've mentioned 'patterns' a few times so far: they're used in `let` bindings,
|
||||||
in function arguments, and in the `match` expression. Patterns have a lot of
|
in function arguments, and in `match` expressions. Patterns have a lot of
|
||||||
abilities, so in this section, we'll cover all of the different things they can
|
abilities, so in this section, we'll cover some of the most commonly used ones.
|
||||||
do. Any of these abilities work in any place where a pattern is used.
|
Any of these abilities work in any place where a pattern is used.
|
||||||
|
|
||||||
## Literals & _
|
## Literals & _
|
||||||
|
|
||||||
@ -84,52 +84,6 @@ match name {
|
|||||||
// than moving it
|
// than moving it
|
||||||
```
|
```
|
||||||
|
|
||||||
## Destructuring
|
|
||||||
|
|
||||||
Patterns can be used to destructure structs and enums:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
struct Point {
|
|
||||||
x: i32,
|
|
||||||
y: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
let origin = Point { x: 0, y: 0 };
|
|
||||||
|
|
||||||
let Point { x, y } = origin;
|
|
||||||
```
|
|
||||||
|
|
||||||
This brings an `x` and `y` binding into scope, matching the `x` and `y` of
|
|
||||||
`origin`. While it can be unusual in `let`, this is the same principle of
|
|
||||||
patterns in `match`:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
struct Point {
|
|
||||||
x: i32,
|
|
||||||
y: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
let origin = Point { x: 0, y: 0 };
|
|
||||||
|
|
||||||
match origin {
|
|
||||||
Point { x, y } => { }, // x and y are bound here
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Shadowing
|
|
||||||
|
|
||||||
As with all bindings, anything bound by a pattern will shadow bindings
|
|
||||||
outside of the binding construct:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
let x = Some(5);
|
|
||||||
|
|
||||||
match x {
|
|
||||||
Some(x) => { }, // x is an i32 here, not an Option<i32>
|
|
||||||
None => (),
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Ignoring bindings
|
## Ignoring bindings
|
||||||
|
|
||||||
We discussed using `_` as a whole pattern to ignore it above, but you can
|
We discussed using `_` as a whole pattern to ignore it above, but you can
|
||||||
@ -188,7 +142,7 @@ Ranges are usually used with integers or `char`s:
|
|||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 'c';
|
let x = 'c';
|
||||||
|
|
||||||
match x {
|
match x {
|
||||||
'a' ... 'j' => println!("early ASCII letter"),
|
'a' ... 'j' => println!("early ASCII letter"),
|
||||||
'k' ... 'z' => println!("late ASCII letter"),
|
'k' ... 'z' => println!("late ASCII letter"),
|
||||||
@ -235,7 +189,3 @@ not this:
|
|||||||
```text
|
```text
|
||||||
4 | (5 if y) => ...
|
4 | (5 if y) => ...
|
||||||
```
|
```
|
||||||
|
|
||||||
## Bindings
|
|
||||||
|
|
||||||
You can bind values to names with `@`:
|
|
||||||
|
Loading…
Reference in New Issue
Block a user