Skip to content

Commit 3f76e29

Browse files
committed
Merge pull request #81 from kw-udon/4_12_enums
4.12. Enums
2 parents f590a12 + 5b9d23f commit 3f76e29

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

1.6/ja/book/enums.md

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
% Enums
1+
% 列挙型
2+
<!-- % Enums -->
23

3-
An `enum` in Rust is a type that represents data that could be one of
4-
several possible variants:
4+
<!-- An `enum` in Rust is a type that represents data that could be one of
5+
several possible variants: -->
6+
Rustの `enum` は、いくつかのヴァリアントのうちからどれか一つをとるデータを表す型です。
57

68
```rust
79
enum Message {
@@ -12,17 +14,27 @@ enum Message {
1214
}
1315
```
1416

15-
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
1618
defining variants resembles the syntaxes used to define structs: you can
1719
have variants with no data (like unit-like structs), variants with named
1820
data, and variants with unnamed data (like tuple structs). Unlike
1921
separate struct definitions, however, an `enum` is a single type. A
2022
value of the enum can match any of the variants. For this reason, an
2123
enum is sometimes called a ‘sum type’: the set of possible values of the
22-
enum is the sum of the sets of possible values for each variant.
23-
24-
We use the `::` syntax to use the name of each variant: they’re scoped by the name
25-
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+
これにより、以下は動きます。
2638

2739
```rust
2840
# enum Message {
@@ -38,36 +50,50 @@ enum BoardGameTurn {
3850
let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 };
3951
```
4052

41-
Both variants are named `Move`, but since they’re scoped to the name of
42-
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` という名前ですが、列挙型の名前でスコープされているため、衝突することなく使うことができます。
4356

44-
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,
4558
in addition to any data associated with that variant. This is sometimes
4659
referred to as a ‘tagged union’, since the data includes a ‘tag’
4760
indicating what type it is. The compiler uses this information to
4861
enforce that you’re accessing the data in the enum safely. For instance,
4962
you can’t simply try to destructure a value as if it were one of the
50-
possible variants:
63+
possible variants: -->
64+
列挙型の値は、ヴァリアントに関連するデータに加え、その値自身がどのヴァリアントであるかという情報を持っています。
65+
これを「タグ付き共用体」(tagged union) ということもあります。
66+
データが、それ自身がどの型なのかを示す「タグ」をもっているためです。
67+
コンパイラはこの情報を用いて、列挙型内のデータへ安全にアクセスすることを強制します。
68+
例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。
5169

5270
```rust,ignore
5371
fn process_color_change(msg: Message) {
54-
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; // コンパイル時エラー
5574
}
5675
```
5776

58-
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
5978
which we can overcome. There are two ways: by implementing equality ourselves,
6079
or by pattern matching variants with [`match`][match] expressions, which you’ll
6180
learn in the next section. We don’t know enough about Rust to implement
62-
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] のセクションに書いてあります。
6386

6487
[match]: match.html
6588
[if-let]: if-let.html
6689
[traits]: traits.html
6790

68-
# Constructors as functions
91+
<!-- # Constructors as functions -->
92+
# 関数としてのコンストラクタ
6993

70-
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+
例えばこうです。
7197

7298
```rust
7399
# enum Message {
@@ -76,7 +102,8 @@ An enum’s constructors can also be used like functions. For example:
76102
let m = Message::Write("Hello, world".to_string());
77103
```
78104

79-
Is the same as
105+
<!-- Is the same as -->
106+
これは、以下と同じです。
80107

81108
```rust
82109
# enum Message {
@@ -89,10 +116,12 @@ fn foo(x: String) -> Message {
89116
let x = foo("Hello, world".to_string());
90117
```
91118

92-
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
93120
[`closures`][closures], we’ll talk about passing functions as arguments to
94121
other functions. For example, with [`iterators`][iterators], we can do this
95-
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` へ変換することができます。
96125

97126
```rust
98127
# enum Message {

TranslationTable.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
| closure | クロージャ
3030
| coercion | 型強制
3131
| comma | カンマ
32+
| compile-time error | コンパイル時エラー
3233
| compiler | コンパイラ
3334
| constant | 定数
35+
| constructor | コンストラクタ
3436
| crate | クレート
3537
| dangling | ダングリング
3638
| declaration statement | 宣言文
@@ -45,6 +47,7 @@
4547
| documentation test | ドキュメンテーションテスト
4648
| early return | 早期リターン
4749
| enum | 列挙型
50+
| equality | 等値性
4851
| expression statement | 式文
4952
| feature | フィーチャ
5053
| foreign | 他言語
@@ -105,9 +108,11 @@
105108
| string interpolation | 文字列インターポーレーション
106109
| struct | 構造体
107110
| structure | ストラクチャ
111+
| sum type | 直和型
108112
| symbol | シンボル
109113
| syntactic sugar | 糖衣構文
110114
| system | システム
115+
| tagged union | タグ付き共用体
111116
| tick | クオート
112117
| trait | トレイト
113118
| type inference | 型推論
@@ -119,6 +124,7 @@
119124
| unsized type | サイズ不定型
120125
| variable | 変数
121126
| variable binding | 変数束縛
127+
| variant | ヴァリアント
122128
| vector | ベクタ
123129
| warning | ウォーニング
124130
| wildcard | ワイルドカード

0 commit comments

Comments
 (0)