Skip to content

Commit 04d1f66

Browse files
committed
Merge pull request #79 from pocket7878/match
4.13. Match
2 parents d819c13 + a00c43a commit 04d1f66

File tree

1 file changed

+62
-34
lines changed

1 file changed

+62
-34
lines changed

1.6/ja/book/match.md

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
% Match
1+
% マッチ
2+
<!-- % Match -->
23

3-
Often, a simple [`if`][if]/`else` isn’t enough, because you have more than two
4-
possible options. Also, conditions can get quite complex. Rust
5-
has a keyword, `match`, that allows you to replace complicated `if`/`else`
6-
groupings with something more powerful. Check it out:
4+
<!-- Often, a simple [`if`][if]/`else` isn’t enough, because you have more than two -->
5+
<!-- possible options. Also, conditions can get quite complex. Rust -->
6+
<!-- has a keyword, `match`, that allows you to replace complicated `if`/`else` -->
7+
<!-- groupings with something more powerful. Check it out: -->
8+
しばしば、2つ以上の可能な処理が存在するためや、分岐条件が非常に複雑になるために単純な [`if`][if]/`else` では充分でない場合があります。
9+
Rustにはキーワード `match` が存在し、複雑な `if`/`else` のグループをさらに強力なもので置き換えられます。
10+
以下の例を見てみましょう:
711

812
```rust
913
let x = 5;
@@ -20,32 +24,49 @@ match x {
2024

2125
[if]: if.html
2226

23-
`match` takes an expression and then branches based on its value. Each ‘arm’ of
24-
the branch is of the form `val => expression`. When the value matches, that arm’s
25-
expression will be evaluated. It’s called `match` because of the term ‘pattern
26-
matching’, which `match` is an implementation of. There’s an [entire section on
27-
patterns][patterns] that covers all the patterns that are possible here.
27+
<!-- `match` takes an expression and then branches based on its value. Each ‘arm’ of -->
28+
<!-- the branch is of the form `val => expression`. When the value matches, that arm’s -->
29+
<!-- expression will be evaluated. It’s called `match` because of the term ‘pattern -->
30+
<!-- matching’, which `match` is an implementation of. There’s an [entire section on -->
31+
<!-- patterns][patterns] that covers all the patterns that are possible here. -->
32+
`match` は一つの式とその式の値に基づく複数のブランチを引数に取ります。
33+
一つ一つの「腕」は `val => expression` という形式を取ります。
34+
値がマッチした時に、対応する腕の式が評価されます。
35+
このような式が `match` と呼ばれるのは「パターンマッチ」に由来します。
36+
可能なすべてのパターンについて説明した、[パターンの説明のために書かれたセクション][patterns] が存在します。
2837

2938
[patterns]: patterns.html
3039

31-
So what’s the big advantage? Well, there are a few. First of all, `match`
32-
enforces ‘exhaustiveness checking’. Do you see that last arm, the one with the
33-
underscore (`_`)? If we remove that arm, Rust will give us an error:
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は以下の様なエラーを発生させます:
3447

3548
```text
3649
error: non-exhaustive patterns: `_` not covered
3750
```
3851

39-
In other words, Rust is trying to tell us we forgot a value. Because `x` is an
40-
integer, Rust knows that it can have a number of different values – for
41-
example, `6`. Without the `_`, however, there is no arm that could match, and
42-
so Rust refuses to compile the code. `_` acts like a ‘catch-all arm’. If none
43-
of the other arms match, the arm with `_` will, and since we have this
44-
catch-all arm, we now have an arm for every possible value of `x`, and so our
45-
program will compile successfully.
46-
47-
`match` is also an expression, which means we can use it on the right-hand
48-
side of a `let` binding or directly where an expression is used:
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` が取り得るすべての値について対応する腕が存在することになり、コンパイルが成功します。
66+
67+
<!-- `match` is also an expression, which means we can use it on the right-hand -->
68+
<!-- side of a `let` binding or directly where an expression is used: -->
69+
`match` は式でも有ります、これはつまり `let` 束縛の右側や式が使われているところで利用することができるということを意味しています。
4970

5071
```rust
5172
let x = 5;
@@ -60,12 +81,15 @@ let number = match x {
6081
};
6182
```
6283

63-
Sometimes it’s a nice way of converting something from one type to another.
84+
<!-- Sometimes it’s a nice way of converting something from one type to another. -->
85+
`match` はしばしば、ある型からある型へ変換するための良い手段になりまうす。
6486

65-
# Matching on enums
87+
<!-- # Matching on enums -->
88+
# 列挙型に対するマッチ
6689

67-
Another important use of the `match` keyword is to process the possible
68-
variants of an enum:
90+
<!-- Another important use of the `match` keyword is to process the possible -->
91+
<!-- variants of an enum: -->
92+
`match` の他の重要な利用方法としては列挙型のバリアントを処理することがあります:
6993

7094
```rust
7195
enum Message {
@@ -89,12 +113,16 @@ fn process_message(msg: Message) {
89113
}
90114
```
91115

92-
Again, the Rust compiler checks exhaustiveness, so it demands that you
93-
have a match arm for every variant of the enum. If you leave one off, it
94-
will give you a compile-time error unless you use `_`.
95-
96-
Unlike the previous uses of `match`, you can’t use the normal `if`
97-
statement to do this. You can use the [`if let`][if-let] statement,
98-
which can be seen as an abbreviated form of `match`.
116+
<!-- Again, the Rust compiler checks exhaustiveness, so it demands that you -->
117+
<!-- have a match arm for every variant of the enum. If you leave one off, it -->
118+
<!-- will give you a compile-time error unless you use `_`. -->
119+
繰り返しになりますが、Rustコンパイラは網羅性のチェックを行い、列挙型のすべてのバリアントに対して、マッチする腕が存在することを要求します。
120+
もし、一つでもマッチする腕のないバリアントを残している場合、 `_` を用いなければコンパイルエラーが発生します。
121+
122+
<!-- Unlike the previous uses of `match`, you can’t use the normal `if` -->
123+
<!-- statement to do this. You can use the [`if let`][if-let] statement, -->
124+
<!-- which can be seen as an abbreviated form of `match`. -->
125+
上で説明した値に対する `match` の利用とは異なり、列挙型のバリアントに基いた分岐に `if` を用いることはできません。
126+
列挙型のバリアントに基いた分岐に [`if let`][if-let] 文を用いることが可能です、 `if let``match` の短縮形と捉えることができます。
99127

100128
[if-let]: if-let.html

0 commit comments

Comments
 (0)