Skip to content

Commit f930107

Browse files
committed
Merge pull request #38 from Nnwww/4_18_Generics
4.18. Generics
2 parents 4051137 + 00971f5 commit f930107

File tree

2 files changed

+58
-34
lines changed

2 files changed

+58
-34
lines changed

1.6/ja/book/generics.md

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
% Generics
1+
% ジェネリクス
2+
<!-- % Generics -->
23

3-
Sometimes, when writing a function or data type, we may want it to work for
4+
<!-- Sometimes, when writing a function or data type, we may want it to work for
45
multiple types of arguments. In Rust, we can do this with generics.
56
Generics are called ‘parametric polymorphism’ in type theory,
67
which means that they are types or functions that have multiple forms (‘poly’
7-
is multiple, ‘morph’ is form) over a given parameter (‘parametric’).
8+
is multiple, ‘morph’ is form) over a given parameter (‘parametric’). -->
9+
時々、関数やデータ型を書いていると、引数が複数の型に対応したものが欲しくなることもあります。Rustでは、ジェネリクスを用いてこれを実現しています。ジェネリクスは型理論において「パラメトリック多相」(parametric polymorphism)と呼ばれ、与えられたパラメータにより(「parametric」)型もしくは関数が多数の様相(「poly」は多様、「morph」は様相を意味します)(訳注: ここで「様相」は型を指します)を持つことを意味しています。
810

9-
Anyway, enough type theory, let’s check out some generic code. Rust’s
10-
standard library provides a type, `Option<T>`, that’s generic:
11+
<!-- Anyway, enough type theory, let’s check out some generic code. Rust’s
12+
standard library provides a type, `Option<T>`, that’s generic: -->
13+
さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。Rustが標準ライブラリで提供している型 `Option<T>` はジェネリックです。
1114

1215
```rust
1316
enum Option<T> {
@@ -16,38 +19,43 @@ enum Option<T> {
1619
}
1720
```
1821

19-
The `<T>` part, which you’ve seen a few times before, indicates that this is
22+
<!-- The `<T>` part, which you’ve seen a few times before, indicates that this is
2023
a generic data type. Inside the declaration of our `enum`, wherever we see a `T`,
2124
we substitute that type for the same type used in the generic. Here’s an
22-
example of using `Option<T>`, with some extra type annotations:
25+
example of using `Option<T>`, with some extra type annotations: -->
26+
`<T>` の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。 `enum` の宣言内であれば、どこでも `T` を使うことができ、宣言内に登場する同じ型をジェネリック内で `T` 型に置き換えています。型注釈を用いた`Option<T>`の使用例が以下になります。
2327

2428
```rust
2529
let x: Option<i32> = Some(5);
2630
```
2731

28-
In the type declaration, we say `Option<i32>`. Note how similar this looks to
32+
<!-- In the type declaration, we say `Option<i32>`. Note how similar this looks to
2933
`Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On
3034
the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`.
3135
Since that’s an `i32`, the two sides match, and Rust is happy. If they didn’t
32-
match, we’d get an error:
36+
match, we’d get an error: -->
37+
この型宣言では `Option<i32>` と書かれています。 `Option<T>` の違いに注目して下さい。そう、上記の `Option` では `T` の値は `i32` です。この束縛の右辺の `Some(T)` では、 `T``5` となります。それが `i32` なので、両辺の型が一致するため、Rustは満足します。型が不一致であれば、以下のようなエラーが発生します。
3338

3439
```rust,ignore
3540
let x: Option<f64> = Some(5);
3641
// error: mismatched types: expected `core::option::Option<f64>`,
3742
// found `core::option::Option<_>` (expected f64 but found integral variable)
3843
```
3944

40-
That doesn’t mean we can’t make `Option<T>`s that hold an `f64`! They just have
41-
to match up:
45+
<!-- That doesn’t mean we can’t make `Option<T>`s that hold an `f64`! They just have
46+
to match up: -->
47+
これは `f64` を保持する `Option<T>` が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。
4248

4349
```rust
4450
let x: Option<i32> = Some(5);
4551
let y: Option<f64> = Some(5.0f64);
4652
```
4753

48-
This is just fine. One definition, multiple uses.
54+
<!-- This is just fine. One definition, multiple uses. -->
55+
これだけで結構です。1つの定義で、多くの用途が得られます。
4956

50-
Generics don’t have to only be generic over one type. Consider another type from Rust’s standard library that’s similar, `Result<T, E>`:
57+
<!-- Generics don’t have to only be generic over one type. Consider another type from Rust’s standard library that’s similar, `Result<T, E>`: -->
58+
ジェネリクスにおいてジェネリックな型は1つまで、といった制限はありません。Rustの標準ライブラリに入っている類似の型 `Result<T, E>` について考えてみます。
5159

5260
```rust
5361
enum Result<T, E> {
@@ -56,8 +64,9 @@ enum Result<T, E> {
5664
}
5765
```
5866

59-
This type is generic over _two_ types: `T` and `E`. By the way, the capital letters
60-
can be any letter you’d like. We could define `Result<T, E>` as:
67+
<!-- This type is generic over _two_ types: `T` and `E`. By the way, the capital letters
68+
can be any letter you’d like. We could define `Result<T, E>` as: -->
69+
この型では `T``E`_2つ_ がジェネリックです。ちなみに、大文字の部分はあなたの好きな文字で構いません。もしあなたが望むなら `Result<T, E>` を、
6170

6271
```rust
6372
enum Result<A, Z> {
@@ -66,44 +75,54 @@ enum Result<A, Z> {
6675
}
6776
```
6877

69-
if we wanted to. Convention says that the first generic parameter should be
70-
`T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however.
78+
<!-- if we wanted to. Convention says that the first generic parameter should be
79+
`T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however. -->
80+
のように定義できます。慣習としては、「Type」から第1ジェネリックパラメータは `T` であるべきですし、「Error」から `E` を用いるのですが、Rustは気にしません。
7181

72-
The `Result<T, E>` type is intended to be used to return the result of a
73-
computation, and to have the ability to return an error if it didn’t work out.
82+
<!-- The `Result<T, E>` type is intended to be used to return the result of a
83+
computation, and to have the ability to return an error if it didn’t work out. -->
84+
`Result<T, E>` 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。
7485

75-
## Generic functions
86+
<!-- ## Generic functions -->
87+
## ジェネリック関数
7688

77-
We can write functions that take generic types with a similar syntax:
89+
<!-- We can write functions that take generic types with a similar syntax: -->
90+
似た構文でジェネリックな型を取る関数を記述できます。
7891

7992
```rust
8093
fn takes_anything<T>(x: T) {
81-
// do something with x
94+
# // do something with x
95+
// xで何か行う
8296
}
8397
```
8498

85-
The syntax has two parts: the `<T>` says “this function is generic over one
86-
type, `T`”, and the `x: T` says “x has the type `T`.”
99+
<!-- The syntax has two parts: the `<T>` says “this function is generic over one
100+
type, `T`”, and the `x: T` says “x has the type `T`.” -->
101+
構文は2つのパーツから成ります。 `<T>` は「この関数は1つの型、 `T` に対してジェネリックである」ということであり、 `x: T` は「xは `T` 型である」という意味です。
87102

88-
Multiple arguments can have the same generic type:
103+
<!-- Multiple arguments can have the same generic type: -->
104+
複数の引数が同じジェネリックな型を持つこともできます。
89105

90106
```rust
91107
fn takes_two_of_the_same_things<T>(x: T, y: T) {
92108
// ...
93109
}
94110
```
95111

96-
We could write a version that takes multiple types:
112+
<!-- We could write a version that takes multiple types: -->
113+
複数の型を取るバージョンを記述することも可能です。
97114

98115
```rust
99116
fn takes_two_things<T, U>(x: T, y: U) {
100117
// ...
101118
}
102119
```
103120

104-
## Generic structs
121+
<!-- ## Generic structs -->
122+
## ジェネリック構造体
105123

106-
You can store a generic type in a `struct` as well:
124+
<!-- You can store a generic type in a `struct` as well: -->
125+
また、 `struct` 内にジェネリックな型の値を保存することもできます。
107126

108127
```rust
109128
struct Point<T> {
@@ -115,11 +134,13 @@ let int_origin = Point { x: 0, y: 0 };
115134
let float_origin = Point { x: 0.0, y: 0.0 };
116135
```
117136

118-
Similar to functions, the `<T>` is where we declare the generic parameters,
119-
and we then use `x: T` in the type declaration, too.
137+
<!-- Similar to functions, the `<T>` is where we declare the generic parameters,
138+
and we then use `x: T` in the type declaration, too. -->
139+
関数と同様に、 `<T>` がジェネリックパラメータを宣言する場所であり、型宣言において `x: T` を使うのも同じです。
120140

121-
When you want to add an implementation for the generic `struct`, you just
122-
declare the type parameter after the `impl`:
141+
<!-- When you want to add an implementation for the generic `struct`, you just
142+
declare the type parameter after the `impl`: -->
143+
ジェネリックな `struct` に実装を追加したい場合、 `impl` の後に型パラメータを宣言するだけです。
123144

124145
```rust
125146
# struct Point<T> {
@@ -134,11 +155,12 @@ impl<T> Point<T> {
134155
}
135156
```
136157

137-
So far you’ve seen generics that take absolutely any type. These are useful in
158+
<!-- So far you’ve seen generics that take absolutely any type. These are useful in
138159
many cases: you’ve already seen `Option<T>`, and later you’ll meet universal
139160
container types like [`Vec<T>`][Vec]. On the other hand, often you want to
140161
trade that flexibility for increased expressive power. Read about [trait
141-
bounds][traits] to see why and how.
162+
bounds][traits] to see why and how. -->
163+
ここまででありとあらゆる型をとることのできるジェネリクスについて見てきました。多くの場合これらは有用です。 `Option<T>` は既に見た通りですし、のちに `Vec<T>` のような普遍的なコンテナ型を知ることになるでしょう。一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。それは何故か、そしてその方法を知るためには [トレイト境界][traits] を読んで下さい。
142164

143165
[traits]: traits.html
144166
[Vec]: ../std/vec/struct.Vec.html

TranslationTable.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| binding | 束縛
2222
| block | ブロック
2323
| borrowing | 借用
24+
| bounds | 境界
2425
| capture | キャプチャ
2526
| closure | クロージャ
2627
| coercion | 型強制
@@ -57,6 +58,7 @@
5758
| owner | 所有者
5859
| ownership | 所有権
5960
| panic | パニック
61+
| parametric polymorphism | パラメトリック多相
6062
| return | 返す
6163
| shadow | 覆い隠す
6264
| signature | シグネチャ

0 commit comments

Comments
 (0)