From 4f4ad2d3d37fd4b3822f1df62f6d94adf32a479e Mon Sep 17 00:00:00 2001 From: Keiichi Watanabe Date: Thu, 11 Feb 2016 20:58:33 +0900 Subject: [PATCH 1/3] Start translation of enums.md --- 1.6/ja/book/enums.md | 8 +++++--- TranslationTable.md | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/1.6/ja/book/enums.md b/1.6/ja/book/enums.md index 8ad4eeed..bf616118 100644 --- a/1.6/ja/book/enums.md +++ b/1.6/ja/book/enums.md @@ -1,7 +1,9 @@ -% Enums +% 列挙型 + -An `enum` in Rust is a type that represents data that could be one of -several possible variants: + +Rustの `enum` は、いくつかのヴァリアントの候補のうちのどれか一つであるようなデータを表す型です。 ```rust enum Message { diff --git a/TranslationTable.md b/TranslationTable.md index e3e96066..90398c82 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -115,6 +115,7 @@ | unsized type | サイズ不定型 | variable | 変数 | variable binding | 変数束縛 +| variant | ヴァリアント | vector | ベクタ | warning | ウォーニング | wildcard | ワイルドカード From 7ec8e51c81336901588d576736d7dbd774e8a319 Mon Sep 17 00:00:00 2001 From: Keiichi Watanabe Date: Fri, 12 Feb 2016 02:08:41 +0900 Subject: [PATCH 2/3] Translate enums.md --- 1.6/ja/book/enums.md | 63 +++++++++++++++++++++++++++++++------------- TranslationTable.md | 5 ++++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/1.6/ja/book/enums.md b/1.6/ja/book/enums.md index bf616118..2ef096f6 100644 --- a/1.6/ja/book/enums.md +++ b/1.6/ja/book/enums.md @@ -3,7 +3,7 @@ -Rustの `enum` は、いくつかのヴァリアントの候補のうちのどれか一つであるようなデータを表す型です。 +Rustの `enum` は、いくつかのヴァリアントのうちからどれか一つをとるデータを表す型です。 ```rust enum Message { @@ -14,17 +14,27 @@ enum Message { } ``` -Each variant can optionally have data associated with it. The syntax for + +各ヴァリアントは、自身に関連するデータを持つこともできます。 +ヴァリアントの定義のための構文は、構造体を定義するのに使われる構文と似ており、 +(unit-like構造体のような) データを持たないヴァリアント、名前付きデータを持つヴァリアント、 (タプル構造体のような) 名前なしデータを持つヴァリアントがありえます。 +しかし、別々に構造体を定義する場合とは異なり、 `enum` は一つの型です。 +列挙型の値はどのヴァリアントにもマッチしうるのです。 +このことから、列挙型は「直和型」(sum type) と呼ばれることもあります。 +列挙型としてありうる値の集合は、各ヴァリアントとしてありうる値の集合の和であるためです。 + + +各ヴァリアントの名前を使うためには、 `::` 構文を使います。 +`enum` 自体の名前によってスコープするのです。 +これにより、以下は動きます。 ```rust # enum Message { @@ -40,36 +50,50 @@ enum BoardGameTurn { let y: BoardGameTurn = BoardGameTurn::Move { squares: 1 }; ``` -Both variants are named `Move`, but since they’re scoped to the name of -the enum, they can both be used without conflict. + +どちらのヴァリアントも `Move` という名前ですが、列挙型の名前でスコープされているため、衝突することなく使うことができます。 -A value of an enum type contains information about which variant it is, + +列挙型の値は、ヴァリアントに関連するデータに加え、その値自身がどのヴァリアントであるかという情報を持っています。 +これを「タグ付きユニオン」(tagged union) ということもあります。 +データが、それ自身がどの型なのかを示す「タグ」をもっているためです。 +コンパイラはこの情報を用いて、列挙型内のデータへ安全にアクセスすることを強制します。 +例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。 ```rust,ignore fn process_color_change(msg: Message) { - let Message::ChangeColor(r, g, b) = msg; // compile-time error +# // let Message::ChangeColor(r, g, b) = msg; // compile-time error + let Message::ChangeColor(r, g, b) = msg; // コンパイル時エラー } ``` -Not supporting these operations may seem rather limiting, but it’s a limitation + +こういった操作が許されないことで制限されているように感じられるかもしれませんが、この制限は克服できます。 +それには二つの方法があります。 +一つは等値性を自分で実装する方法、もう一つは次のセクションで学ぶ [`match`][match] 式でヴァリアントのパターンマッチを行う方法です。 +等値性を実装するにはRustについてまだ知るべきことがありますが、 [`トレイト`][traits] のセクションに書いてあります。 [match]: match.html [if-let]: if-let.html [traits]: traits.html -# Constructors as functions + +# 関数としてのコンストラクタ -An enum’s constructors can also be used like functions. For example: + +列挙型のコンストラクタも、関数のように使うことができます。 +例えばこうです。 ```rust # enum Message { @@ -78,7 +102,8 @@ An enum’s constructors can also be used like functions. For example: let m = Message::Write("Hello, world".to_string()); ``` -Is the same as + +これは、以下と同じです。 ```rust # enum Message { @@ -91,10 +116,12 @@ fn foo(x: String) -> Message { let x = foo("Hello, world".to_string()); ``` -This is not immediately useful to us, but when we get to + +また、すぐに役立つことではないのですが、[`クロージャ`][closures] までいくと、関数を他の関数へ引数として渡す話をします。 +例えば、これを [`イテレータ`][iterators] とあわせることで、 `String` のベクタから `Message::Write` へ変換することができます。 ```rust # enum Message { diff --git a/TranslationTable.md b/TranslationTable.md index 90398c82..f3cbf3c0 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -29,8 +29,10 @@ | closure | クロージャ | coercion | 型強制 | comma | カンマ +| compile-time error | コンパイル時エラー | compiler | コンパイラ | constant | 定数 +| constructor | コンストラクタ | crate | クレート | dangling | ダングリング | declaration statement | 宣言文 @@ -45,6 +47,7 @@ | documentation test | ドキュメンテーションテスト | early return | 早期リターン | enum | 列挙型 +| equality | 等値性 | expression statement | 式文 | feature | フィーチャ | generic parameter | ジェネリックパラメータ @@ -102,9 +105,11 @@ | string interpolation | 文字列インターポーレーション | struct | 構造体 | structure | ストラクチャ +| sum type | 直和型 | symbol | シンボル | syntactic sugar | 糖衣構文 | system | システム +| tagged union | タグ付きユニオン | tick | クオート | trait | トレイト | type inference | 型推論 From 5b9d23fd2a1ce44436e937c84c8960fd921025b4 Mon Sep 17 00:00:00 2001 From: Keiichi Watanabe Date: Fri, 12 Feb 2016 18:19:38 +0900 Subject: [PATCH 3/3] =?UTF-8?q?"=E3=82=BF=E3=82=B0=E4=BB=98=E3=81=8D?= =?UTF-8?q?=E3=83=A6=E3=83=8B=E3=82=AA=E3=83=B3"=20->=20"=E3=82=BF?= =?UTF-8?q?=E3=82=B0=E4=BB=98=E3=81=8D=E5=85=B1=E7=94=A8=E4=BD=93"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.6/ja/book/enums.md | 2 +- TranslationTable.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/1.6/ja/book/enums.md b/1.6/ja/book/enums.md index 2ef096f6..391339d0 100644 --- a/1.6/ja/book/enums.md +++ b/1.6/ja/book/enums.md @@ -62,7 +62,7 @@ enforce that you’re accessing the data in the enum safely. For instance, you can’t simply try to destructure a value as if it were one of the possible variants: --> 列挙型の値は、ヴァリアントに関連するデータに加え、その値自身がどのヴァリアントであるかという情報を持っています。 -これを「タグ付きユニオン」(tagged union) ということもあります。 +これを「タグ付き共用体」(tagged union) ということもあります。 データが、それ自身がどの型なのかを示す「タグ」をもっているためです。 コンパイラはこの情報を用いて、列挙型内のデータへ安全にアクセスすることを強制します。 例えば、値をどれか一つのヴァリアントであるかのようにみなして、その中身を取り出すということはできません。 diff --git a/TranslationTable.md b/TranslationTable.md index f3cbf3c0..2887d935 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -109,7 +109,7 @@ | symbol | シンボル | syntactic sugar | 糖衣構文 | system | システム -| tagged union | タグ付きユニオン +| tagged union | タグ付き共用体 | tick | クオート | trait | トレイト | type inference | 型推論