2
2
<!-- % Patterns -->
3
3
4
4
<!-- Patterns are quite common in Rust. We use them in [variable -->
5
- <!-- bindings][bindings], [match statements ][match], and other places, too. Let’s go -->
5
+ <!-- bindings][bindings], [match expressions ][match], and other places, too. Let’s go -->
6
6
<!-- on a whirlwind tour of all of the things patterns can do!-->
7
7
パターンはRustにおいて極めて一般的です。
8
8
パターンは [ 変数束縛] [ bindings ] , [ マッチ文] [ match ] などで使われています。
9
-
10
9
さあ、めくるめくパターンの旅を始めましょう!
11
10
12
11
[ bindings ] : variable-bindings.html
13
12
[ match ] : match.html
14
13
15
14
<!-- A quick refresher: you can match against literals directly, and `_` acts as an
16
15
‘any’ case: -->
17
- 簡単な復習:リテラルに対しては直接マッチさせられます。また、 ` _ ` は「任意の」ケースとして振る舞います。
16
+ 簡単な復習:リテラルに対しては直接マッチ出来ます。
17
+ また、 ` _ ` は「任意の」ケースとして振る舞います。
18
18
19
19
``` rust
20
20
let x = 1 ;
@@ -32,10 +32,12 @@ match x {
32
32
33
33
<!-- There’s one pitfall with patterns: like anything that introduces a new binding, -->
34
34
<!-- they introduce shadowing. For example: -->
35
- パターンには一つ落とし穴があります。新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングします。例えば:
35
+ パターンには一つ落とし穴があります。
36
+ 新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングします。
37
+ 例えば:
36
38
37
39
``` rust
38
- let x = 'x' ;
40
+ let x = 1 ;
39
41
let c = 'c' ;
40
42
41
43
match c {
@@ -50,13 +52,18 @@ println!("x: {}", x)
50
52
51
53
``` text
52
54
x: c c: c
53
- x: x
55
+ x: 1
54
56
```
55
57
56
58
<!-- In other words, `x =>` matches the pattern and introduces a new binding named -->
57
- <!-- `x` that’s in scope for the match arm. Because we already have a binding named -->
58
- <!-- `x`, this new `x` shadows it. -->
59
- 別の言い方をすると、 ` x => ` はパターンへのマッチだけでなく、マッチの腕内で有効な ` x ` という名前の束縛を導入します。既に ` x ` は束縛されていたので、この新しい ` x ` はそれを覆い隠します。
59
+ <!-- `x`. This new binding is in scope for the match arm and takes on the value of -->
60
+ <!-- `c`. Notice that the value of `x` outside the scope of the match has no bearing -->
61
+ <!-- on the value of `x` within it. Because we already have a binding named `x`, this -->
62
+ <!-- new `x` shadows it. -->
63
+ 別の言い方をすると、 ` x => ` はパターンへにマッチし ` x ` という名前の束縛を導入します。
64
+ この束縛はマッチの腕で有効で、 ` c ` の値を引き受けます。
65
+ マッチのスコープ外にある ` x ` は内部での ` x ` の値に関係していないことに注意して下さい。
66
+ 既に ` x ` は束縛されていたので、この新しい ` x ` はそれを覆い隠します。
60
67
61
68
<!-- # Multiple patterns -->
62
69
# 複式パターン
@@ -83,7 +90,7 @@ match x {
83
90
84
91
<!-- If you have a compound data type, like a [`struct`][struct], you can destructure it -->
85
92
<!-- inside of a pattern: -->
86
- 例えば [ ` struct ` ] [ struct ] のような複合データ型を作成したいとき 、パターン内でデータを分解することができます。
93
+ 例えば [ ` struct ` ] [ struct ] のような複合データ型があるとき 、パターン内でデータを分解することができます。
87
94
88
95
``` rust
89
96
struct Point {
@@ -135,8 +142,8 @@ match origin {
135
142
<!-- This prints `x is 0`. -->
136
143
これは ` x is 0 ` を出力します。
137
144
138
- <!-- You can do this kind of match on any member, not just the first:-->
139
- どのメンバに対してもこの種のマッチを行うことができます。たとえ最初ではなくても:
145
+ <!-- You can do this kind of match on any member, not only the first: -->
146
+ 最初のものだけでなく、 どのメンバに対してもこの種のマッチを行うことができます。
140
147
141
148
``` rust
142
149
struct Point {
@@ -154,8 +161,8 @@ match origin {
154
161
<!-- This prints `y is 0`. -->
155
162
これは ` y is 0 ` を出力します。
156
163
157
- <!-- This ‘destructuring’ behavior works on any compound data type, like
158
- [tuples][tuples] or [enums][enums]. -->
164
+ <!-- This ‘destructuring’ behavior works on any compound data type, like -->
165
+ <!-- [tuples][tuples] or [enums][enums]. -->
159
166
この「デストラクチャリング(destructuring)」と呼ばれる振る舞いは、 [ タプル] [ tuples ] や [ 列挙型] [ enums ] のような、複合データ型で使用できます。
160
167
161
168
[ tuples ] : primitive-types.html#tuples
@@ -178,10 +185,10 @@ match some_value {
178
185
```
179
186
180
187
<!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But -->
181
- <!-- in the `Err` arm, we use `_` to disregard the specific error, and just print -->
188
+ <!-- in the `Err` arm, we use `_` to disregard the specific error, and print -->
182
189
<!-- a general error message. -->
183
- 最初の部分では ` Ok ` ヴァリアント内の値を ` value ` に結びつけています 。
184
- しかし ` Err ` 部分では、特定のエラーを避けて、標準的なエラーメッセージを表示するために ` _ ` を使っています 。
190
+ 最初の部分では ` Ok ` ヴァリアント内の値を ` value ` に束縛しています 。
191
+ しかし ` Err ` 部分では、` _ ` を使って特定のエラーを無視し、一般的なエラーメッセージを表示しています 。
185
192
186
193
<!-- `_` is valid in any pattern that creates a binding. This can be useful to -->
187
194
<!-- ignore parts of a larger structure: -->
@@ -198,12 +205,50 @@ fn coordinate() -> (i32, i32, i32) {
198
205
let (x , _ , z ) = coordinate ();
199
206
```
200
207
201
- <!-- Here, we bind the first and last element of the tuple to `x` and `z`, but
202
- ignore the middle element. -->
208
+ <!-- Here, we bind the first and last element of the tuple to `x` and `z`, but -->
209
+ <!-- ignore the middle element. -->
203
210
ここでは、タプルの最初と最後の要素を ` x ` と ` z ` に結びつけています。
204
211
205
- <!-- Similarly, you can use `..` in a pattern to disregard multiple values. -->
206
- 同様に ` .. ` でパターン内の複数の値を無視することができます。
212
+ <!-- It’s worth noting that using `_` never binds the value in the first place, -->
213
+ <!-- which means a value may not move: -->
214
+ ` _ ` はそもそも値を束縛しない、つまり値がムーブしないということは特筆に値します。
215
+
216
+ ``` rust
217
+ let tuple : (u32 , String ) = (5 , String :: from (" five" ));
218
+
219
+ # // Here, tuple is moved, because the String moved:
220
+ // これだとタプルはムーブします。何故ならStringがムーブしているからです:
221
+ let (x , _s ) = tuple ;
222
+
223
+ # // The next line would give "error: use of partially moved value: `tuple`"
224
+ // この行は「error: use of partially moved value: `tuple`」を出します。
225
+ // println!("Tuple is: {:?}", tuple);
226
+
227
+ // However,
228
+
229
+ let tuple = (5 , String :: from (" five" ));
230
+
231
+ // Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy:
232
+ // これだとタプルはムーブ _されません_ 。何故ならStringはムーブされず、u32はCopyだからです:
233
+ let (x , _ ) = tuple ;
234
+
235
+ # // That means this works:
236
+ // つまりこれは動きます:
237
+ println! (" Tuple is: {:?}" , tuple );
238
+ ```
239
+
240
+ <!-- This also means that any temporary variables will be dropped at the end of the -->
241
+ <!-- statement: -->
242
+ また、束縛されていない値は文の終わりでドロップされるということでもあります。
243
+
244
+ ``` rust
245
+ # // Here, the String created will be dropped immediately, as it’s not bound:
246
+ // 生成されたStringは束縛されていないので即座にドロップされる
247
+ let _ = String :: from (" hello " ). trim ();
248
+ ```
249
+
250
+ <!-- You can also use `..` in a pattern to disregard multiple values: -->
251
+ 複数の値を捨てるのには ` .. ` パターンが使えます。
207
252
208
253
``` rust
209
254
enum OptionalTuple {
@@ -236,16 +281,17 @@ match x {
236
281
}
237
282
```
238
283
239
- <!-- This prints `Got a reference to 5`. -->
284
+ <!-- This prints `Got a reference to 5`. -->
240
285
これは ` Got a reference to 5 ` を出力します。
241
286
242
287
[ ref ] : references-and-borrowing.html
243
288
244
- <!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
245
- keyword _creates_ a reference, for use in the pattern. If you need a mutable
246
- reference, `ref mut` will work in the same way: -->
289
+ <!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` -->
290
+ <!-- keyword _creates_ a reference, for use in the pattern. If you need a mutable -->
291
+ <!-- reference, `ref mut` will work in the same way: -->
247
292
ここで ` match ` 内の ` r ` は ` &i32 ` 型を持っています。
248
- 言い換えると ` ref ` キーワードがリファレンスを _ 作ります_ 。
293
+ 言い換えると ` ref ` キーワードがパターン内で使うリファレンスを _ 作ります_ 。
294
+ ミュータブルなリファレンスが必要なら、 ` ref mut ` が同じように動作します。
249
295
250
296
``` rust
251
297
let mut x = 5 ;
@@ -293,7 +339,7 @@ match x {
293
339
# 束縛
294
340
295
341
<!-- You can bind values to names with `@`: -->
296
- ` @ ` で値を名前と結びつけることができます 。
342
+ ` @ ` で値を名前に束縛することができます 。
297
343
298
344
``` rust
299
345
let x = 1 ;
@@ -304,8 +350,8 @@ match x {
304
350
}
305
351
```
306
352
307
- <!-- This prints `got a range element 1`. This is useful when you want to
308
- do a complicated match of part of a data structure: -->
353
+ <!-- This prints `got a range element 1`. This is useful when you want to -->
354
+ <!-- do a complicated match of part of a data structure: -->
309
355
これは ` got a range element 1 ` を出力します。
310
356
データ構造の一部に対する複雑なマッチが欲しいときに有用です:
311
357
@@ -316,19 +362,19 @@ struct Person {
316
362
}
317
363
318
364
let name = " Steve" . to_string ();
319
- let mut x : Option <Person > = Some (Person { name : Some (name ) });
365
+ let x : Option <Person > = Some (Person { name : Some (name ) });
320
366
match x {
321
367
Some (Person { name : ref a @ Some (_ ), .. }) => println! (" {:?}" , a ),
322
368
_ => {}
323
369
}
324
370
```
325
371
326
- <!-- This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
372
+ <!-- This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
327
373
これは ` Some("Steve") ` を出力します。内側の ` name ` を ` a ` に結びつけます。
328
374
329
- <!-- If you use `@` with `|`, you need to make sure the name is bound in each part
330
- of the pattern: -->
331
- もし ` | ` で ` @ ` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります:
375
+ <!-- If you use `@` with `|`, you need to make sure the name is bound in each part -->
376
+ <!-- of the pattern: -->
377
+ もし ` | ` で ` @ ` を使うときは、必ずそれぞれのパターンが名前と結びついている必要があります。
332
378
333
379
``` rust
334
380
let x = 5 ;
@@ -342,7 +388,7 @@ match x {
342
388
<!-- # Guards -->
343
389
# ガード
344
390
345
- <!-- You can introduce ‘match guards’ with `if`: -->
391
+ <!-- You can introduce ‘match guards’ with `if`: -->
346
392
` if ` を使うことでマッチガードを導入することができます:
347
393
348
394
``` rust
@@ -360,10 +406,10 @@ match x {
360
406
}
361
407
```
362
408
363
- <!-- This prints `Got an int!`. -->
409
+ <!-- This prints `Got an int!`. -->
364
410
これは ` Got an int! ` を出力します。
365
411
366
- <!-- If you’re using `if` with multiple patterns, the `if` applies to both sides:-->
412
+ <!-- If you’re using `if` with multiple patterns, the `if` applies to both sides: -->
367
413
複式パターンで ` if ` を使うと、 ` if ` は両方に適用されます:
368
414
369
415
``` rust
@@ -376,8 +422,8 @@ match x {
376
422
}
377
423
```
378
424
379
- <!-- This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
380
- just the `5`. In other words, the precedence of `if` behaves like this: -->
425
+ <!-- This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to -->
426
+ <!-- only the `5`. In other words, the precedence of `if` behaves like this: -->
381
427
これは ` no ` を出力します。なぜなら ` if ` は ` 4 | 5 ` 全体に適用されるのであって、 ` 5 ` 単独に対してではないからです。つまり ` if ` 節は以下のように振舞います:
382
428
383
429
``` text
@@ -394,8 +440,8 @@ just the `5`. In other words, the precedence of `if` behaves like this: -->
394
440
<!-- # Mix and Match -->
395
441
# 混ぜてマッチ
396
442
397
- <!-- Whew! That’s a lot of different ways to match things, and they can all be
398
- mixed and matched, depending on what you’re doing: -->
443
+ <!-- Whew! That’s a lot of different ways to match things, and they can all be -->
444
+ <!-- mixed and matched, depending on what you’re doing: -->
399
445
ふう、マッチには様々な方法があるのですね。やりたいこと次第で、それらを混ぜてマッチさせることもできます:
400
446
401
447
``` rust,ignore
0 commit comments