33
44<!-- An `enum` in Rust is a type that represents data that could be one of
55several possible variants: -->
6- Rustの ` enum ` は、いくつかのヴァリアントの候補のうちのどれか一つであるようなデータを表す型です 。
6+ Rustの ` enum ` は、いくつかのヴァリアントのうちからどれか一つをとるデータを表す型です 。
77
88``` rust
99enum Message {
@@ -14,17 +14,27 @@ enum Message {
1414}
1515```
1616
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
1818defining variants resembles the syntaxes used to define structs: you can
1919have variants with no data (like unit-like structs), variants with named
2020data, and variants with unnamed data (like tuple structs). Unlike
2121separate struct definitions, however, an `enum` is a single type. A
2222value of the enum can match any of the variants. For this reason, an
2323enum 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+ これにより、以下は動きます。
2838
2939``` rust
3040# enum Message {
@@ -40,36 +50,50 @@ enum BoardGameTurn {
4050let y : BoardGameTurn = BoardGameTurn :: Move { squares : 1 };
4151```
4252
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 ` という名前ですが、列挙型の名前へスコープされているため、衝突することなく使うことができます。
4556
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,
4758in addition to any data associated with that variant. This is sometimes
4859referred to as a ‘tagged union’, since the data includes a ‘tag’
4960indicating what type it is. The compiler uses this information to
5061enforce that you’re accessing the data in the enum safely. For instance,
5162you 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+ 例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。
5369
5470``` rust,ignore
5571fn 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; // コンパイル時エラー
5774}
5875```
5976
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
6178which we can overcome. There are two ways: by implementing equality ourselves,
6279or by pattern matching variants with [`match`][match] expressions, which you’ll
6380learn 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 ] のセクションに書いてあります。
6586
6687[ match ] : match.html
6788[ if-let ] : if-let.html
6889[ traits ] : traits.html
6990
70- # Constructors as functions
91+ <!-- # Constructors as functions -->
92+ # 関数としてのコンストラクタ
7193
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+ 例えばこうです。
7397
7498``` rust
7599# enum Message {
@@ -78,7 +102,8 @@ An enum’s constructors can also be used like functions. For example:
78102let m = Message :: Write (" Hello, world" . to_string ());
79103```
80104
81- Is the same as
105+ <!-- Is the same as -->
106+ これは、以下と同じです。
82107
83108``` rust
84109# enum Message {
@@ -91,10 +116,12 @@ fn foo(x: String) -> Message {
91116let x = foo (" Hello, world" . to_string ());
92117```
93118
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
95120[`closures`][closures], we’ll talk about passing functions as arguments to
96121other 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 ` へ変換することができます。
98125
99126``` rust
100127# enum Message {
0 commit comments