1
- % Generics
1
+ % ジェネリクス
2
+ <!-- % Generics -->
2
3
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
4
5
multiple types of arguments. In Rust, we can do this with generics.
5
6
Generics are called ‘parametric polymorphism’ in type theory,
6
7
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」は様相を意味します)(訳注: ここで「様相」は型を指します)を持つことを意味しています。
8
10
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> ` はジェネリックです。
11
14
12
15
``` rust
13
16
enum Option <T > {
@@ -16,38 +19,43 @@ enum Option<T> {
16
19
}
17
20
```
18
21
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
20
23
a generic data type. Inside the declaration of our `enum`, wherever we see a `T`,
21
24
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> ` の使用例が以下になります。
23
27
24
28
``` rust
25
29
let x : Option <i32 > = Some (5 );
26
30
```
27
31
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
29
33
`Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On
30
34
the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`.
31
35
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は満足します。型が不一致であれば、以下のようなエラーが発生します。
33
38
34
39
``` rust,ignore
35
40
let x: Option<f64> = Some(5);
36
41
// error: mismatched types: expected `core::option::Option<f64>`,
37
42
// found `core::option::Option<_>` (expected f64 but found integral variable)
38
43
```
39
44
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> ` が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。
42
48
43
49
``` rust
44
50
let x : Option <i32 > = Some (5 );
45
51
let y : Option <f64 > = Some (5.0f64 );
46
52
```
47
53
48
- This is just fine. One definition, multiple uses.
54
+ <!-- This is just fine. One definition, multiple uses. -->
55
+ これだけで結構です。1つの定義で、多くの用途が得られます。
49
56
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> ` について考えてみます。
51
59
52
60
``` rust
53
61
enum Result <T , E > {
@@ -56,8 +64,9 @@ enum Result<T, E> {
56
64
}
57
65
```
58
66
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> ` を、
61
70
62
71
``` rust
63
72
enum Result <A , Z > {
@@ -66,44 +75,54 @@ enum Result<A, Z> {
66
75
}
67
76
```
68
77
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は気にしません。
71
81
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> ` 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。
74
85
75
- ## Generic functions
86
+ <!-- ## Generic functions -->
87
+ ## ジェネリック関数
76
88
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
+ 似た構文でジェネリックな型を取る関数を記述できます。
78
91
79
92
``` rust
80
93
fn takes_anything <T >(x : T ) {
81
- // do something with x
94
+ # // do something with x
95
+ // xで何か行う
82
96
}
83
97
```
84
98
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 ` 型である」という意味です。
87
102
88
- Multiple arguments can have the same generic type:
103
+ <!-- Multiple arguments can have the same generic type: -->
104
+ 複数の引数が同じジェネリックな型を持つこともできます。
89
105
90
106
``` rust
91
107
fn takes_two_of_the_same_things <T >(x : T , y : T ) {
92
108
// ...
93
109
}
94
110
```
95
111
96
- We could write a version that takes multiple types:
112
+ <!-- We could write a version that takes multiple types: -->
113
+ 複数の型を取るバージョンを記述することも可能です。
97
114
98
115
``` rust
99
116
fn takes_two_things <T , U >(x : T , y : U ) {
100
117
// ...
101
118
}
102
119
```
103
120
104
- ## Generic structs
121
+ <!-- ## Generic structs -->
122
+ ## ジェネリック構造体
105
123
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 ` 内にジェネリックな型の値を保存することもできます。
107
126
108
127
``` rust
109
128
struct Point <T > {
@@ -115,11 +134,13 @@ let int_origin = Point { x: 0, y: 0 };
115
134
let float_origin = Point { x : 0.0 , y : 0.0 };
116
135
```
117
136
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 ` を使うのも同じです。
120
140
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 ` の後に型パラメータを宣言するだけです。
123
144
124
145
``` rust
125
146
# struct Point <T > {
@@ -134,11 +155,12 @@ impl<T> Point<T> {
134
155
}
135
156
```
136
157
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
138
159
many cases: you’ve already seen `Option<T>`, and later you’ll meet universal
139
160
container types like [`Vec<T>`][Vec]. On the other hand, often you want to
140
161
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 ] を読んで下さい。
142
164
143
165
[ traits ] : traits.html
144
166
[ Vec ] : ../std/vec/struct.Vec.html
0 commit comments