1
- % Associated Types
2
-
3
- Associated types are a powerful part of Rust’s type system. They’re related to
4
- the idea of a ‘type family’, in other words, grouping multiple types together. That
5
- description is a bit abstract, so let’s dive right into an example. If you want
6
- to write a ` Graph ` trait, you have two types to be generic over: the node type
7
- and the edge type. So you might write a trait, ` Graph<N, E> ` , that looks like
8
- this:
1
+ % 関連型
2
+ <!-- % Associated Types -->
3
+
4
+ <!-- Associated types are a powerful part of Rust’s type system. They’re related to -->
5
+ <!-- the idea of a ‘type family’, in other words, grouping multiple types together. That -->
6
+ <!-- description is a bit abstract, so let’s dive right into an example. If you want -->
7
+ <!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
8
+ <!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
9
+ <!-- this: -->
10
+ 関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
11
+ 言い換えると、複数の型をグループ化するものです。
12
+ この説明はすこし抽象的なので、実際の例を見ていきましょう。
13
+ 例えば、 ` Graph ` トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
14
+ そのため、以下のように ` Graph<N, E> ` と書きたくなるでしょう:
9
15
10
16
``` rust
11
17
trait Graph <N , E > {
@@ -15,19 +21,24 @@ trait Graph<N, E> {
15
21
}
16
22
```
17
23
18
- While this sort of works, it ends up being awkward. For example, any function
19
- that wants to take a ` Graph ` as a parameter now _ also_ needs to be generic over
20
- the ` N ` ode and ` E ` dge types too:
24
+ <!-- While this sort of works, it ends up being awkward. For example, any function -->
25
+ <!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
26
+ <!-- the `N`ode and `E`dge types too: -->
27
+ たしかに上のようなコードは動作しますが、この ` Graph ` の定義は少し扱いづらいです。
28
+ たとえば、任意の ` Graph ` を引数に取る関数は、 _ 同時に_ 頂点 ` N ` と辺 ` E ` についてもジェネリックとなることになります:
21
29
22
30
``` rust,ignore
23
31
fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
24
32
```
25
33
26
- Our distance calculation works regardless of our ` Edge ` type, so the ` E ` stuff in
27
- this signature is just a distraction.
34
+ <!-- Our distance calculation works regardless of our `Edge` type, so the `E` stuff in -->
35
+ <!-- this signature is just a distraction. -->
36
+ この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる ` E ` に関連する部分は邪魔でしかありません。
28
37
29
- What we really want to say is that a certain ` E ` dge and ` N ` ode type come together
30
- to form each kind of ` Graph ` . We can do that with associated types:
38
+ <!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
39
+ <!-- to form each kind of `Graph`. We can do that with associated types: -->
40
+ 本当に表現したいことは、それぞれのグラフ( ` Graph ` )は辺( ` E ` )や頂点( ` N ` )で構成されているということです。
41
+ それは、以下のように関連型を用いて表現することができます:
31
42
32
43
``` rust
33
44
trait Graph {
@@ -40,19 +51,25 @@ trait Graph {
40
51
}
41
52
```
42
53
43
- Now, our clients can be abstract over a given ` Graph ` :
54
+ <!-- Now, our clients can be abstract over a given `Graph`: -->
55
+ このようにすると、` Graph ` を使った関数は以下のように書くことができます:
44
56
45
57
``` rust,ignore
46
58
fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
47
59
```
48
60
49
- No need to deal with the ` E ` dge type here!
61
+ <!-- No need to deal with the `E`dge type here! -->
62
+ もう ` E ` について扱う必要はありません!
63
+
50
64
51
- Let’s go over all this in more detail.
65
+ <!-- Let’s go over all this in more detail. -->
66
+ もっと詳しく見ていきましょう。
52
67
53
- ## Defining associated types
68
+ <!-- ## Defining associated types -->
69
+ ## 関連型を定義する
54
70
55
- Let’s build that ` Graph ` trait. Here’s the definition:
71
+ <!-- Let’s build that `Graph` trait. Here’s the definition: -->
72
+ 早速、` Graph ` トレイトを定義しましょう。以下がその定義です:
56
73
57
74
``` rust
58
75
trait Graph {
@@ -64,12 +81,15 @@ trait Graph {
64
81
}
65
82
```
66
83
67
- Simple enough. Associated types use the ` type ` keyword, and go inside the body
68
- of the trait, with the functions.
84
+ <!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
85
+ <!-- of the trait, with the functions. -->
86
+ 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体や関数で利用します。
69
87
70
- These ` type ` declarations can have all the same thing as functions do. For example,
71
- if we wanted our ` N ` type to implement ` Display ` , so we can print the nodes out,
72
- we could do this:
88
+ <!-- These `type` declarations can have all the same thing as functions do. For example, -->
89
+ <!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
90
+ <!-- we could do this: -->
91
+ これらの ` type ` 宣言は、関数で利用できるものと同じものが全て利用できます。
92
+ たとえば、 ` N ` 型が ` Display ` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます:
73
93
74
94
``` rust
75
95
use std :: fmt;
@@ -83,10 +103,13 @@ trait Graph {
83
103
}
84
104
```
85
105
86
- ## Implementing associated types
106
+ <!-- ## Implementing associated types -->
107
+ ## 関連型を実装する
87
108
88
- Just like any trait, traits that use associated types use the ` impl ` keyword to
89
- provide implementations. Here’s a simple implementation of Graph:
109
+ <!-- Just like any trait, traits that use associated types use the `impl` keyword to -->
110
+ <!-- provide implementations. Here’s a simple implementation of Graph: -->
111
+ 通常のトレイトと同様に、関連型を使っているトレイトは実装するために ` impl ` を利用します。
112
+ 以下は、シンプルなGraphの実装例です:
90
113
91
114
``` rust
92
115
# trait Graph {
@@ -115,23 +138,34 @@ impl Graph for MyGraph {
115
138
}
116
139
```
117
140
118
- This silly implementation always returns ` true ` and an empty ` Vec<Edge> ` , but it
119
- gives you an idea of how to implement this kind of thing. We first need three
120
- ` struct ` s, one for the graph, one for the node, and one for the edge. If it made
121
- more sense to use a different type, that would work as well, we’re just going to
122
- use ` struct ` s for all three here.
141
+ <!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
142
+ <!-- gives you an idea of how to implement this kind of thing. We first need three -->
143
+ <!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
144
+ <!-- more sense to use a different type, that would work as well, we’re just going to -->
145
+ <!-- use `struct`s for all three here. -->
146
+ この奇妙な実装は、つねに ` true ` と空の ` Vec<Edge> ` を返しますますが、どのように定義したら良いかのアイデアをくれます。
147
+ まず、はじめに3つの ` struct ` が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
148
+ もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの ` struct ` を用います。
149
+
123
150
124
- Next is the ` impl ` line, which is just like implementing any other trait.
151
+ <!-- Next is the `impl` line, which is just like implementing any other trait. -->
152
+ 次は ` impl ` の行です、これは他のトレイトを実装するときと同様です。
125
153
126
- From here, we use ` = ` to define our associated types. The name the trait uses
127
- goes on the left of the ` = ` , and the concrete type we’re ` impl ` ementing this
128
- for goes on the right. Finally, we use the concrete types in our function
129
- declarations.
154
+ <!-- From here, we use `=` to define our associated types. The name the trait uses -->
155
+ <!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
156
+ <!-- for goes on the right. Finally, we use the concrete types in our function -->
157
+ <!-- declarations. -->
158
+ そして、` = ` を関連型を定義するために利用します。
159
+ トレイトが利用する名前は ` = ` の左側にある名前で、実装に用いる具体的な型は右側にあるものになります。
160
+ 最後に、具体的な型を関数の宣言に利用します。
130
161
131
- ## Trait objects with associated types
162
+ <!-- ## Trait objects with associated types -->
163
+ ## 関連型を伴うトレイト
132
164
133
- There’s one more bit of syntax we should talk about: trait objects. If you
134
- try to create a trait object from an associated type, like this:
165
+ <!-- There’s one more bit of syntax we should talk about: trait objects. If you -->
166
+ <!-- try to create a trait object from an associated type, like this: -->
167
+ すこし触れておきたい構文: トレイトオブジェクト が有ります。
168
+ もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合:
135
169
136
170
``` rust,ignore
137
171
# trait Graph {
@@ -157,7 +191,8 @@ let graph = MyGraph;
157
191
let obj = Box::new(graph) as Box<Graph>;
158
192
```
159
193
160
- You’ll get two errors:
194
+ <!-- You’ll get two errors: -->
195
+ 以下の様なエラーが発生します:
161
196
162
197
``` text
163
198
error: the value of the associated type `E` (from the trait `main::Graph`) must
@@ -170,8 +205,10 @@ let obj = Box::new(graph) as Box<Graph>;
170
205
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171
206
```
172
207
173
- We can’t create a trait object like this, because we don’t know the associated
174
- types. Instead, we can write this:
208
+ <!-- We can’t create a trait object like this, because we don’t know the associated -->
209
+ <!-- types. Instead, we can write this: -->
210
+ 上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
211
+ 代わりに以下のように書くことができます:
175
212
176
213
``` rust
177
214
# trait Graph {
@@ -197,6 +234,8 @@ let graph = MyGraph;
197
234
let obj = Box :: new (graph ) as Box <Graph <N = Node , E = Edge >>;
198
235
```
199
236
200
- The ` N=Node ` syntax allows us to provide a concrete type, ` Node ` , for the ` N `
201
- type parameter. Same with ` E=Edge ` . If we didn’t provide this constraint, we
202
- couldn’t be sure which ` impl ` to match this trait object to.
237
+ <!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
238
+ <!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
239
+ <!-- couldn’t be sure which `impl` to match this trait object to. -->
240
+ ` N=Node ` 構文を用いて型パラメータ ` N ` にたいして具体的な型 ` Node ` を指定することができます、` E=Edge ` についても同様です。
241
+ もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの ` impl ` がマッチするのか定まりません。
0 commit comments