@@ -27,7 +27,7 @@ match x {
2727<!-- `match` takes an expression and then branches based on its value. Each ‘arm’ of -->
2828<!-- the branch is of the form `val => expression`. When the value matches, that arm’s -->
2929<!-- expression will be evaluated. It’s called `match` because of the term ‘pattern -->
30- <!-- matching’, which `match` is an implementation of. There’s a [separate section on -->
30+ <!-- matching’, which `match` is an implementation of. There’s an [entire section on -->
3131<!-- patterns][patterns] that covers all the patterns that are possible here. -->
3232` match ` は一つの式とその式の値に基づく複数のブランチを引数に取ります。
3333一つ一つの「腕」は ` val => expression ` という形式を取ります。
@@ -37,25 +37,32 @@ match x {
3737
3838[ patterns ] : patterns.html
3939
40- <!-- One of the many advantages of `match` is it enforces ‘exhaustiveness checking’. -->
41- <!-- For example if we remove the last arm with the underscore `_`, the compiler will -->
42- <!-- give us an error: -->
43- 数ある ` match ` の利点のうちの一つに「網羅性検査」を行なうということが上げられます。
44- 例えば最後の ` _ ` の腕を消すと、コンパイラはエラーを出します。
40+ <!-- So what’s the big advantage? Well, there are a few. First of all, `match` -->
41+ <!-- enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the -->
42+ <!-- underscore (`_`)? If we remove that arm, Rust will give us an error: -->
43+ ` match ` を使う利点は何でしょうか? いくつか有りますが、
44+ まず一つ目としては ` match ` をつかうことで、「網羅性検査」が実施されます。
45+ 上のコードで、最後のアンダースコア( ` _ ` )を用いている腕があるのがわかりますか?
46+ もし、その腕を削除した場合、Rustは以下の様なエラーを発生させます:
4547
4648``` text
4749error: non-exhaustive patterns: `_` not covered
4850```
4951
50- <!-- Rust is telling us that we forgot some value. The compiler infers from `x` that it -->
51- <!-- can have any 32bit integer value; for example -2,147,483,648 to 2,147,483,647. The `_` acts -->
52- <!-- as a 'catch-all', and will catch all possible values that *aren't* specified in -->
53- <!-- an arm of `match`. As you can see in the previous example, we provide `match` -->
54- <!-- arms for integers 1-5, if `x` is 6 or any other value, then it is caught by `_`. -->
55- Rustは何かしらの値を忘れていると教えてくれています。
56- コンパイラは ` x ` が任意の32bitの値、例えば-2,147,483,648から2,147,483,647を取り得ると推論します。
57- ` _ ` が「がらくた入れ」として振舞います、 ` match ` の腕で指定され * なかった* 可能な値全てを捕捉します。
58- 先の例で見た通り、 ` match ` の腕は 1〜5の値を書いたので、 ` x ` が6、あるいは他の値だった時は ` _ ` に捕捉されます。
52+ <!-- In other words, Rust is trying to tell us we forgot a value. Because `x` is an -->
53+ <!-- integer, Rust knows that it can have a number of different values – for -->
54+ <!-- example, `6`. Without the `_`, however, there is no arm that could match, and -->
55+ <!-- so Rust refuses to compile the code. `_` acts like a ‘catch-all arm’. If none -->
56+ <!-- of the other arms match, the arm with `_` will, and since we have this -->
57+ <!-- catch-all arm, we now have an arm for every possible value of `x`, and so our -->
58+ <!-- program will compile successfully. -->
59+ 言い換えると、Rustは値を忘れていることを伝えようとしているのです。
60+ なぜなら ` x ` は整数であるため、Rustは ` x ` は多くの異なる値を取ることができることを知っています。
61+ 例えば、 ` 6 ` などがそれにに当たります。
62+ もし ` _ ` がなかった場合、 ` 6 ` にマッチする腕が存在しない事になります、そのためRustはコンパイルを通しません。
63+ ` _ ` は「全てキャッチする腕」のように振る舞います。
64+ もし他の腕がどれもマッチしなかった場合、 ` _ ` の腕が実行されることになります、
65+ この「全てキャッチする腕」が存在するため、 ` x ` が取り得るすべての値について対応する腕が存在することになり、コンパイルが成功します。
5966
6067<!-- `match` is also an expression, which means we can use it on the right-hand -->
6168<!-- side of a `let` binding or directly where an expression is used: -->
@@ -74,10 +81,8 @@ let number = match x {
7481};
7582```
7683
77- <!-- Sometimes it’s a nice way of converting something from one type to another; in -->
78- <!-- this example the integers are converted to `String`. -->
84+ <!-- Sometimes it’s a nice way of converting something from one type to another. -->
7985` match ` はしばしば、ある型からある型へ変換するための良い手段になります。
80- この例では整数が ` String ` に変換されています。
8186
8287<!-- # Matching on enums -->
8388# 列挙型に対するマッチ
@@ -110,10 +115,9 @@ fn process_message(msg: Message) {
110115
111116<!-- Again, the Rust compiler checks exhaustiveness, so it demands that you -->
112117<!-- have a match arm for every variant of the enum. If you leave one off, it -->
113- <!-- will give you a compile-time error unless you use `_` or provide all possible -->
114- <!-- arms. -->
118+ <!-- will give you a compile-time error unless you use `_`. -->
115119繰り返しになりますが、Rustコンパイラは網羅性のチェックを行い、列挙型のすべてのバリアントに対して、マッチする腕が存在することを要求します。
116- もし、一つでもマッチする腕のないバリアントを残している場合、 ` _ ` を用いるか可能な腕を全て書くかしなければコンパイルエラーが発生します 。
120+ もし、一つでもマッチする腕のないバリアントを残している場合、 ` _ ` を用いなければコンパイルエラーが発生します 。
117121
118122<!-- Unlike the previous uses of `match`, you can’t use the normal `if` -->
119123<!-- statement to do this. You can use the [`if let`][if-let] statement, -->
0 commit comments