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