Skip to content

Commit 20a7771

Browse files
committed
Translate enum.md
1 parent 4f4ad2d commit 20a7771

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

1.6/ja/book/enums.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

88
```rust
99
enum 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
1818
defining variants resembles the syntaxes used to define structs: you can
1919
have variants with no data (like unit-like structs), variants with named
2020
data, and variants with unnamed data (like tuple structs). Unlike
2121
separate struct definitions, however, an `enum` is a single type. A
2222
value of the enum can match any of the variants. For this reason, an
2323
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+
これにより、以下は動きます。
2838

2939
```rust
3040
# enum Message {
@@ -40,36 +50,50 @@ enum BoardGameTurn {
4050
let 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,
4758
in addition to any data associated with that variant. This is sometimes
4859
referred to as a ‘tagged union’, since the data includes a ‘tag’
4960
indicating what type it is. The compiler uses this information to
5061
enforce that you’re accessing the data in the enum safely. For instance,
5162
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+
例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。
5369

5470
```rust,ignore
5571
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; // コンパイル時エラー
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
6178
which we can overcome. There are two ways: by implementing equality ourselves,
6279
or by pattern matching variants with [`match`][match] expressions, which you’ll
6380
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] のセクションに書いてあります。
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:
78102
let 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 {
91116
let 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
96121
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` へ変換することができます。
98125

99126
```rust
100127
# enum Message {

TranslationTable.md

Lines changed: 5 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
| generic parameter | ジェネリックパラメータ
@@ -102,9 +105,11 @@
102105
| string interpolation | 文字列インターポーレーション
103106
| struct | 構造体
104107
| structure | ストラクチャ
108+
| sum type | 直和型
105109
| symbol | シンボル
106110
| syntactic sugar | 糖衣構文
107111
| system | システム
112+
| tagged union | タグ付きユニオン
108113
| tick | クオート
109114
| trait | トレイト
110115
| type inference | 型推論

0 commit comments

Comments
 (0)