3
3
4
4
<!-- An `enum` in Rust is a type that represents data that could be one of
5
5
several possible variants: -->
6
- Rustの ` enum ` は、いくつかのヴァリアントの候補のうちのどれか一つであるようなデータを表す型です 。
6
+ Rustの ` enum ` は、いくつかのヴァリアントのうちからどれか一つをとるデータを表す型です 。
7
7
8
8
``` rust
9
9
enum Message {
@@ -14,17 +14,27 @@ enum Message {
14
14
}
15
15
```
16
16
17
- Each variant can optionally have data associated with it. The syntax for
17
+ <!-- Each variant can optionally have data associated with it. The syntax for
18
18
defining variants resembles the syntaxes used to define structs: you can
19
19
have variants with no data (like unit-like structs), variants with named
20
20
data, and variants with unnamed data (like tuple structs). Unlike
21
21
separate struct definitions, however, an `enum` is a single type. A
22
22
value of the enum can match any of the variants. For this reason, an
23
23
enum is sometimes called a ‘sum type’: the set of possible values of the
24
- enum is the sum of the sets of possible values for each variant.
25
-
26
- We use the ` :: ` syntax to use the name of each variant: they’re scoped by the name
27
- of the ` enum ` itself. This allows both of these to work:
24
+ enum is the sum of the sets of possible values for each variant. -->
25
+ 各ヴァリアントは、自身に関連するデータを持つこともできます。
26
+ ヴァリアントの定義のための構文は、構造体を定義するのに使われる構文と似ており、
27
+ (unit-like構造体のような) データを持たないヴァリアント、名前付きデータを持つヴァリアント、 (タプル構造体のような) 名前なしデータを持つヴァリアントがありえます。
28
+ しかし、別々に構造体を定義する場合とは異なり、 ` enum ` は一つの型です。
29
+ 列挙型の値はどのヴァリアントにもマッチしうるのです。
30
+ このことから、列挙型は「直和型」(sum type) と呼ばれることもあります。
31
+ 列挙型としてありうる値の集合は、各ヴァリアントとしてありうる値の集合の和であるためです。
32
+
33
+ <!-- We use the `::` syntax to use the name of each variant: they’re scoped by the name
34
+ of the `enum` itself. This allows both of these to work: -->
35
+ 各ヴァリアントの名前を使うためには、 ` :: ` 構文を使います。
36
+ ` enum ` 自体の名前によってスコープするのです。
37
+ これにより、以下は動きます。
28
38
29
39
``` rust
30
40
# enum Message {
@@ -40,36 +50,50 @@ enum BoardGameTurn {
40
50
let y : BoardGameTurn = BoardGameTurn :: Move { squares : 1 };
41
51
```
42
52
43
- Both variants are named ` Move ` , but since they’re scoped to the name of
44
- the enum, they can both be used without conflict.
53
+ <!-- Both variants are named `Move`, but since they’re scoped to the name of
54
+ the enum, they can both be used without conflict. -->
55
+ どちらのヴァリアントも ` Move ` という名前ですが、列挙型の名前へスコープされているため、衝突することなく使うことができます。
45
56
46
- A value of an enum type contains information about which variant it is,
57
+ <!-- A value of an enum type contains information about which variant it is,
47
58
in addition to any data associated with that variant. This is sometimes
48
59
referred to as a ‘tagged union’, since the data includes a ‘tag’
49
60
indicating what type it is. The compiler uses this information to
50
61
enforce that you’re accessing the data in the enum safely. For instance,
51
62
you can’t simply try to destructure a value as if it were one of the
52
- possible variants:
63
+ possible variants: -->
64
+ 列挙型の値は、ヴァリアントに関連するデータに加え、その値自身がどのヴァリアントであるかという情報を持っています。
65
+ これを「タグ付きユニオン」(tagged union) ということもあります。
66
+ データが、それ自身がどの型なのかを示す「タグ」をもっているためです。
67
+ コンパイラはこの情報を用いて、列挙型内のデータへ安全にアクセスすることを強制します。
68
+ 例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。
53
69
54
70
``` rust,ignore
55
71
fn process_color_change(msg: Message) {
56
- let Message::ChangeColor(r, g, b) = msg; // compile-time error
72
+ # // let Message::ChangeColor(r, g, b) = msg; // compile-time error
73
+ let Message::ChangeColor(r, g, b) = msg; // コンパイル時エラー
57
74
}
58
75
```
59
76
60
- Not supporting these operations may seem rather limiting, but it’s a limitation
77
+ <!-- Not supporting these operations may seem rather limiting, but it’s a limitation
61
78
which we can overcome. There are two ways: by implementing equality ourselves,
62
79
or by pattern matching variants with [`match`][match] expressions, which you’ll
63
80
learn in the next section. We don’t know enough about Rust to implement
64
- equality yet, but we’ll find out in the [ ` traits ` ] [ traits ] section.
81
+ equality yet, but we’ll find out in the [`traits`][traits] section. -->
82
+ こういった操作が許されないことで制限されているように感じられるかもしれませんが、この制限は克服できます。
83
+ それには二つの方法があります。
84
+ 一つは等値性を自分で実装する方法、もう一つは次のセクションで学ぶ [ ` match ` ] [ match ] 式でヴァリアントのパターンマッチを行う方法です。
85
+ 等値性を実装するにはRustについてまだ知るべきことがありますが、 [ ` トレイト ` ] [ traits ] のセクションに書いてあります。
65
86
66
87
[ match ] : match.html
67
88
[ if-let ] : if-let.html
68
89
[ traits ] : traits.html
69
90
70
- # Constructors as functions
91
+ <!-- # Constructors as functions -->
92
+ # 関数としてのコンストラクタ
71
93
72
- An enum’s constructors can also be used like functions. For example:
94
+ <!-- An enum’s constructors can also be used like functions. For example: -->
95
+ 列挙型のコンストラクタも、関数のように使うことができます。
96
+ 例えばこうです。
73
97
74
98
``` rust
75
99
# enum Message {
@@ -78,7 +102,8 @@ An enum’s constructors can also be used like functions. For example:
78
102
let m = Message :: Write (" Hello, world" . to_string ());
79
103
```
80
104
81
- Is the same as
105
+ <!-- Is the same as -->
106
+ これは、以下と同じです。
82
107
83
108
``` rust
84
109
# enum Message {
@@ -91,10 +116,12 @@ fn foo(x: String) -> Message {
91
116
let x = foo (" Hello, world" . to_string ());
92
117
```
93
118
94
- This is not immediately useful to us, but when we get to
119
+ <!-- This is not immediately useful to us, but when we get to
95
120
[`closures`][closures], we’ll talk about passing functions as arguments to
96
121
other functions. For example, with [`iterators`][iterators], we can do this
97
- to convert a vector of ` String ` s into a vector of ` Message::Write ` s:
122
+ to convert a vector of `String`s into a vector of `Message::Write`s: -->
123
+ また、すぐに役立つことではないのですが、[ ` クロージャ ` ] [ closures ] までいくと、関数を他の関数へ引数として渡す話をします。
124
+ 例えば、これを [ ` イテレータ ` ] [ iterators ] とあわせることで、 ` String ` のベクタから ` Message::Write ` へ変換することができます。
98
125
99
126
``` rust
100
127
# enum Message {
0 commit comments