1
1
% 列挙型
2
2
<!-- % Enums -->
3
3
4
- <!-- An `enum` in Rust is a type that represents data that could be one of
5
- several possible variants: -->
4
+ <!-- An `enum` in Rust is a type that represents data that is one of
5
+ several possible variants. Each variant in the `enum` can optionally
6
+ have data associated with it: -->
6
7
Rustの ` enum ` は、いくつかのヴァリアントのうちからどれか一つをとるデータを表す型です。
8
+ ` enum ` の各ヴァリアントは、それぞれ自身に関連するデータを持つこともできます。
7
9
8
10
``` rust
9
11
enum Message {
@@ -14,15 +16,13 @@ enum Message {
14
16
}
15
17
```
16
18
17
- <!-- Each variant can optionally have data associated with it. The syntax for
18
- defining variants resembles the syntaxes used to define structs: you can
19
- have variants with no data (like unit-like structs), variants with named
19
+ <!-- The syntax for defining variants resembles the syntaxes used to define structs:
20
+ you can have variants with no data (like unit-like structs), variants with named
20
21
data, and variants with unnamed data (like tuple structs). Unlike
21
22
separate struct definitions, however, an `enum` is a single type. A
22
23
value of the enum can match any of the variants. For this reason, an
23
24
enum is sometimes called a ‘sum type’: the set of possible values of the
24
25
enum is the sum of the sets of possible values for each variant. -->
25
- 各ヴァリアントは、自身に関連するデータを持つこともできます。
26
26
ヴァリアントの定義のための構文は、構造体を定義するのに使われる構文と似ており、
27
27
(unit-like構造体のような) データを持たないヴァリアント、名前付きデータを持つヴァリアント、 (タプル構造体のような) 名前なしデータを持つヴァリアントがありえます。
28
28
しかし、別々に構造体を定義する場合とは異なり、 ` enum ` は一つの型です。
@@ -54,7 +54,7 @@ let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
54
54
the enum, they can both be used without conflict. -->
55
55
どちらのヴァリアントも ` Move ` という名前ですが、列挙型の名前でスコープ化されているため、衝突することなく使うことができます。
56
56
57
- <!-- 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,
58
58
in addition to any data associated with that variant. This is sometimes
59
59
referred to as a ‘tagged union’, since the data includes a ‘tag’
60
60
indicating what type it is. The compiler uses this information to
@@ -85,13 +85,12 @@ equality yet, but we’ll find out in the [`traits`][traits] section. -->
85
85
等値性を実装するにはRustについてまだ知るべきことがありますが、 [ ` トレイト ` ] [ traits ] のセクションに書いてあります。
86
86
87
87
[ match ] : match.html
88
- [ if-let ] : if-let.html
89
88
[ traits ] : traits.html
90
89
91
90
<!-- # Constructors as functions -->
92
91
# 関数としてのコンストラクタ
93
92
94
- <!-- An enum’s constructors can also be used like functions . For example: -->
93
+ <!-- An ` enum` constructor can also be used like a function . For example: -->
95
94
列挙型のコンストラクタも、関数のように使うことができます。
96
95
例えばこうです。
97
96
@@ -102,7 +101,7 @@ equality yet, but we’ll find out in the [`traits`][traits] section. -->
102
101
let m = Message :: Write (" Hello, world" . to_string ());
103
102
```
104
103
105
- <!-- Is the same as -->
104
+ <!-- is the same as -->
106
105
これは、以下と同じです。
107
106
108
107
``` rust
0 commit comments