@@ -229,7 +229,7 @@ <h1 class="title">マッチ</h1>
229
229
230
230
<!-- expression will be evaluated. It’s called `match` because of the term ‘pattern -->
231
231
232
- <!-- matching’, which `match` is an implementation of. There’s a [separate section on -->
232
+ <!-- matching’, which `match` is an implementation of. There’s an [entire section on -->
233
233
234
234
<!-- patterns][patterns] that covers all the patterns that are possible here. -->
235
235
@@ -239,32 +239,41 @@ <h1 class="title">マッチ</h1>
239
239
このような式が < code > match</ code > と呼ばれるのは「パターンマッチ」に由来します。
240
240
可能なすべてのパターンについて説明した、< a href ="patterns.html "> パターンの説明のために書かれたセクション</ a > が存在します。</ p >
241
241
242
- <!-- One of the many advantages of `match` is it enforces ‘exhaustiveness checking’. -->
242
+ <!-- So what’s the big advantage? Well, there are a few. First of all, `match` -->
243
243
244
- <!-- For example if we remove the last arm with the underscore `_`, the compiler will -->
244
+ <!-- enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the -->
245
245
246
- <!-- give us an error: -->
246
+ <!-- underscore (`_`)? If we remove that arm, Rust will give us an error: -->
247
247
248
- < p > 数ある < code > match</ code > の利点のうちの一つに「網羅性検査」を行なうということが上げられます。
249
- 例えば最後の < code > _</ code > の腕を消すと、コンパイラはエラーを出します。</ p >
248
+ < p > < code > match</ code > を使う利点は何でしょうか? いくつか有りますが、
249
+ まず一つ目としては < code > match</ code > をつかうことで、「網羅性検査」が実施されます。
250
+ 上のコードで、最後のアンダースコア( < code > _</ code > )を用いている腕があるのがわかりますか?
251
+ もし、その腕を削除した場合、Rustは以下の様なエラーを発生させます:</ p >
250
252
251
253
< pre > < code class ="language-text "> error: non-exhaustive patterns: `_` not covered
252
254
</ code > </ pre >
253
255
254
- <!-- Rust is telling us that we forgot some value. The compiler infers from `x` that it -->
256
+ <!-- In other words, Rust is trying to tell us we forgot a value. Because `x` is an -->
255
257
256
- <!-- can have any 32bit integer value; for example -2,147,483,648 to 2,147,483,647. The `_` acts -->
258
+ <!-- integer, Rust knows that it can have a number of different values – for -->
257
259
258
- <!-- as a 'catch-all', and will catch all possible values that *aren't* specified in -->
260
+ <!-- example, `6`. Without the `_`, however, there is no arm that could match, and -->
259
261
260
- <!-- an arm of `match`. As you can see in the previous example, we provide `match` -->
262
+ <!-- so Rust refuses to compile the code. `_` acts like a ‘catch-all arm’. If none -->
261
263
262
- <!-- arms for integers 1-5, if `x` is 6 or any other value, then it is caught by `_`. -->
264
+ <!-- of the other arms match, the arm with `_` will, and since we have this -->
263
265
264
- < p > Rustは何かしらの値を忘れていると教えてくれています。
265
- コンパイラは < code > x</ code > が任意の32bitの値、例えば-2,147,483,648から2,147,483,647を取り得ると推論します。
266
- < code > _</ code > が「がらくた入れ」として振舞います、 < code > match</ code > の腕で指定され < em > なかった</ em > 可能な値全てを捕捉します。
267
- 先の例で見た通り、 < code > match</ code > の腕は 1〜5の値を書いたので、 < code > x</ code > が6、あるいは他の値だった時は < code > _</ code > に捕捉されます。</ p >
266
+ <!-- catch-all arm, we now have an arm for every possible value of `x`, and so our -->
267
+
268
+ <!-- program will compile successfully. -->
269
+
270
+ < p > 言い換えると、Rustは値を忘れていることを伝えようとしているのです。
271
+ なぜなら < code > x</ code > は整数であるため、Rustは < code > x</ code > は多くの異なる値を取ることができることを知っています。
272
+ 例えば、 < code > 6</ code > などがそれにに当たります。
273
+ もし < code > _</ code > がなかった場合、 < code > 6</ code > にマッチする腕が存在しない事になります、そのためRustはコンパイルを通しません。
274
+ < code > _</ code > は「全てキャッチする腕」のように振る舞います。
275
+ もし他の腕がどれもマッチしなかった場合、 < code > _</ code > の腕が実行されることになります、
276
+ この「全てキャッチする腕」が存在するため、 < code > x</ code > が取り得るすべての値について対応する腕が存在することになり、コンパイルが成功します。</ p >
268
277
269
278
<!-- `match` is also an expression, which means we can use it on the right-hand -->
270
279
@@ -295,12 +304,9 @@ <h1 class="title">マッチ</h1>
295
304
_ < span class ='op '> =></ span > < span class ='string '> "something else"</ span > ,
296
305
};</ pre >
297
306
298
- <!-- Sometimes it’s a nice way of converting something from one type to another; in -->
307
+ <!-- Sometimes it’s a nice way of converting something from one type to another. -->
299
308
300
- <!-- this example the integers are converted to `String`. -->
301
-
302
- < p > < code > match</ code > はしばしば、ある型からある型へ変換するための良い手段になります。
303
- この例では整数が < code > String</ code > に変換されています。</ p >
309
+ < p > < code > match</ code > はしばしば、ある型からある型へ変換するための良い手段になります。</ p >
304
310
305
311
<!-- # Matching on enums -->
306
312
@@ -356,12 +362,10 @@ <h1 id='列挙型に対するマッチ' class='section-header'><a href='#列挙
356
362
357
363
<!-- have a match arm for every variant of the enum. If you leave one off, it -->
358
364
359
- <!-- will give you a compile-time error unless you use `_` or provide all possible -->
360
-
361
- <!-- arms. -->
365
+ <!-- will give you a compile-time error unless you use `_`. -->
362
366
363
367
< p > 繰り返しになりますが、Rustコンパイラは網羅性のチェックを行い、列挙型のすべてのバリアントに対して、マッチする腕が存在することを要求します。
364
- もし、一つでもマッチする腕のないバリアントを残している場合、 < code > _</ code > を用いるか可能な腕を全て書くかしなければコンパイルエラーが発生します 。</ p >
368
+ もし、一つでもマッチする腕のないバリアントを残している場合、 < code > _</ code > を用いなければコンパイルエラーが発生します 。</ p >
365
369
366
370
<!-- Unlike the previous uses of `match`, you can’t use the normal `if` -->
367
371
0 commit comments