diff --git a/1.6/ja/book/generics.md b/1.6/ja/book/generics.md index 347c1f57..8a168133 100644 --- a/1.6/ja/book/generics.md +++ b/1.6/ja/book/generics.md @@ -1,13 +1,16 @@ -% Generics +% ジェネリクス + -Sometimes, when writing a function or data type, we may want it to work for + +時々、関数やデータ型を書いていると、引数が複数の型に対応したものが欲しくなることもあります。Rustでは、ジェネリクスを用いてこれを実現しています。ジェネリクスは型理論において「パラメトリック多相」(parametric polymorphism)と呼ばれ、与えられたパラメータにより(「parametric」)型もしくは関数が多数の様相(「poly」は多様、「morph」は様相を意味します)(訳注: ここで「様相」は型を指します)を持つことを意味しています。 -Anyway, enough type theory, let’s check out some generic code. Rust’s -standard library provides a type, `Option`, that’s generic: + +さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。Rustが標準ライブラリで提供している型 `Option` はジェネリックです。 ```rust enum Option { @@ -16,20 +19,22 @@ enum Option { } ``` -The `` part, which you’ve seen a few times before, indicates that this is + +`` の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。 `enum` の宣言内であれば、どこでも `T` を使うことができ、宣言内に登場する同じ型をジェネリック内で `T` 型に置き換えています。型注釈を用いた`Option`の使用例が以下になります。 ```rust let x: Option = Some(5); ``` -In the type declaration, we say `Option`. Note how similar this looks to + +この型宣言では `Option` と書かれています。 `Option` の違いに注目して下さい。そう、上記の `Option` では `T` の値は `i32` です。この束縛の右辺の `Some(T)` では、 `T` は `5` となります。それが `i32` なので、両辺の型が一致するため、Rustは満足します。型が不一致であれば、以下のようなエラーが発生します。 ```rust,ignore let x: Option = Some(5); @@ -37,17 +42,20 @@ let x: Option = Some(5); // found `core::option::Option<_>` (expected f64 but found integral variable) ``` -That doesn’t mean we can’t make `Option`s that hold an `f64`! They just have -to match up: + +これは `f64` を保持する `Option` が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。 ```rust let x: Option = Some(5); let y: Option = Some(5.0f64); ``` -This is just fine. One definition, multiple uses. + +これだけで結構です。1つの定義で、多くの用途が得られます。 -Generics don’t have to only be generic over one type. Consider another type from Rust’s standard library that’s similar, `Result`: + +ジェネリクスにおいてジェネリックな型は1つまで、といった制限はありません。Rustの標準ライブラリに入っている類似の型 `Result` について考えてみます。 ```rust enum Result { @@ -56,8 +64,9 @@ enum Result { } ``` -This type is generic over _two_ types: `T` and `E`. By the way, the capital letters -can be any letter you’d like. We could define `Result` as: + +この型では `T` と `E` の _2つ_ がジェネリックです。ちなみに、大文字の部分はあなたの好きな文字で構いません。もしあなたが望むなら `Result` を、 ```rust enum Result { @@ -66,26 +75,33 @@ enum Result { } ``` -if we wanted to. Convention says that the first generic parameter should be -`T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however. + +のように定義できます。慣習としては、「Type」から第1ジェネリックパラメータは `T` であるべきですし、「Error」から `E` を用いるのですが、Rustは気にしません。 -The `Result` type is intended to be used to return the result of a -computation, and to have the ability to return an error if it didn’t work out. + +`Result` 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。 -## Generic functions + +## ジェネリック関数 -We can write functions that take generic types with a similar syntax: + +似た構文でジェネリックな型を取る関数を記述できます。 ```rust fn takes_anything(x: T) { - // do something with x +# // do something with x + // xで何か行う } ``` -The syntax has two parts: the `` says “this function is generic over one -type, `T`”, and the `x: T` says “x has the type `T`.” + +構文は2つのパーツから成ります。 `` は「この関数は1つの型、 `T` に対してジェネリックである」ということであり、 `x: T` は「xは `T` 型である」という意味です。 -Multiple arguments can have the same generic type: + +複数の引数が同じジェネリックな型を持つこともできます。 ```rust fn takes_two_of_the_same_things(x: T, y: T) { @@ -93,7 +109,8 @@ fn takes_two_of_the_same_things(x: T, y: T) { } ``` -We could write a version that takes multiple types: + +複数の型を取るバージョンを記述することも可能です。 ```rust fn takes_two_things(x: T, y: U) { @@ -101,9 +118,11 @@ fn takes_two_things(x: T, y: U) { } ``` -## Generic structs + +## ジェネリック構造体 -You can store a generic type in a `struct` as well: + +また、 `struct` 内にジェネリックな型の値を保存することもできます。 ```rust struct Point { @@ -115,11 +134,13 @@ let int_origin = Point { x: 0, y: 0 }; let float_origin = Point { x: 0.0, y: 0.0 }; ``` -Similar to functions, the `` is where we declare the generic parameters, -and we then use `x: T` in the type declaration, too. + +関数と同様に、 `` がジェネリックパラメータを宣言する場所であり、型宣言において `x: T` を使うのも同じです。 -When you want to add an implementation for the generic `struct`, you just -declare the type parameter after the `impl`: + +ジェネリックな `struct` に実装を追加したい場合、 `impl` の後に型パラメータを宣言するだけです。 ```rust # struct Point { @@ -134,11 +155,12 @@ impl Point { } ``` -So far you’ve seen generics that take absolutely any type. These are useful in + +ここまででありとあらゆる型をとることのできるジェネリクスについて見てきました。多くの場合これらは有用です。 `Option` は既に見た通りですし、のちに `Vec` のような普遍的なコンテナ型を知ることになるでしょう。一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。それは何故か、そしてその方法を知るためには [トレイト境界][traits] を読んで下さい。 [traits]: traits.html [Vec]: ../std/vec/struct.Vec.html diff --git a/TranslationTable.md b/TranslationTable.md index 18170a6a..4d0d309e 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -21,6 +21,7 @@ | binding | 束縛 | block | ブロック | borrowing | 借用 +| bounds | 境界 | capture | キャプチャ | closure | クロージャ | coercion | 型強制 @@ -57,6 +58,7 @@ | owner | 所有者 | ownership | 所有権 | panic | パニック +| parametric polymorphism | パラメトリック多相 | return | 返す | shadow | 覆い隠す | signature | シグネチャ