7
7
<!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
8
8
<!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
9
9
<!-- this: -->
10
- 関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
11
- 言い換えると、複数の型をグループ化するものです。
10
+ 関連型は、Rust型システムの強力な部分です。
11
+ 関連型は、「型族」という概念と関連があり、 言い換えると、複数の型をグループ化するものです。
12
12
この説明はすこし抽象的なので、実際の例を見ていきましょう。
13
13
例えば、 ` Graph ` トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
14
14
そのため、以下のように ` Graph<N, E> ` と書きたくなるでしょう:
@@ -25,20 +25,20 @@ trait Graph<N, E> {
25
25
<!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
26
26
<!-- the `N`ode and `E`dge types too: -->
27
27
たしかに上のようなコードは動作しますが、この ` Graph ` の定義は少し扱いづらいです。
28
- たとえば、任意の ` Graph ` を引数に取る関数は、 _ 同時に _ 頂点 ` N ` と辺 ` E ` についてもジェネリックとなることになります :
28
+ たとえば、任意の ` Graph ` を引数に取る関数は、 _ さらに _ 頂点 ` N ` と辺 ` E ` の型についてもジェネリックになる必要があります :
29
29
30
30
``` rust,ignore
31
31
fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
32
32
```
33
33
34
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 ` に関連する部分は邪魔でしかありません 。
35
+ <!-- this signature is a distraction. -->
36
+ この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる ` E ` に関連する部分は邪魔になります 。
37
37
38
38
<!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
39
39
<!-- to form each kind of `Graph`. We can do that with associated types: -->
40
- 本当に表現したいことは、それぞれのグラフ( ` Graph ` )は辺( ` E ` )や頂点( ` N ` )で構成されているということです 。
41
- それは、以下のように関連型を用いて表現することができます :
40
+ 本当に表現したいのは、それぞれの ` Graph ` は、辺 ` E ` と頂点 ` N ` で構成されていることです 。
41
+ それは、以下のように関連型を用いて表現できます :
42
42
43
43
``` rust
44
44
trait Graph {
@@ -52,15 +52,14 @@ trait Graph {
52
52
```
53
53
54
54
<!-- Now, our clients can be abstract over a given `Graph`: -->
55
- このようにすると、 ` Graph ` を使った関数は以下のように書くことができます :
55
+ こうすると、使う側では、個々の ` Graph ` をより抽象的なものとして扱えます :
56
56
57
57
``` rust,ignore
58
58
fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
59
59
```
60
60
61
61
<!-- No need to deal with the `E`dge type here! -->
62
- もう ` E ` について扱う必要はありません!
63
-
62
+ ここでは、頂点 ` E ` 型を扱わずに済んでいます!
64
63
65
64
<!-- Let’s go over all this in more detail. -->
66
65
もっと詳しく見ていきましょう。
@@ -83,13 +82,13 @@ trait Graph {
83
82
84
83
<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
85
84
<!-- of the trait, with the functions. -->
86
- 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体や関数で利用します 。
85
+ 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体にある関数で利用します 。
87
86
88
87
<!-- These `type` declarations can have all the same thing as functions do. For example, -->
89
88
<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
90
89
<!-- we could do this: -->
91
90
これらの ` type ` 宣言は、関数で利用できるものと同じものが全て利用できます。
92
- たとえば、 ` N ` 型が ` Display ` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます :
91
+ たとえば、頂点を表示するため ` N ` 型には ` Display ` を実装してほしいなら、以下のように指定できます :
93
92
94
93
``` rust
95
94
use std :: fmt;
@@ -141,15 +140,18 @@ impl Graph for MyGraph {
141
140
<!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
142
141
<!-- gives you an idea of how to implement this kind of thing. We first need three -->
143
142
<!-- `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 -->
143
+ <!-- more sense to use a different type, that would work as well, we’re going to -->
145
144
<!-- use `struct`s for all three here. -->
146
- この奇妙な実装は、つねに ` true ` と空の ` Vec<Edge> ` を返しますが、どのように定義したら良いかのアイデアをくれます。
147
- まず、はじめに3つの ` struct ` が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
148
- もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの ` struct ` を用います。
149
-
145
+ この、いささか単純過ぎる実装では、常に ` true ` と空の ` Vec<Edge> ` を返します。
146
+ しかし、関連型をどう定義したらよいのかを教えてくれます。
147
+ まず、はじめに3つの ` struct ` が必要です。
148
+ グラフのためにひとつ、頂点のためにひとつ、辺のためにひとつです。
149
+ もし異なる型を利用するのが適切ならば、そうしても構いません。
150
+ 今回はこの3つの ` struct ` を用います。
150
151
151
- <!-- Next is the `impl` line, which is just like implementing any other trait. -->
152
- 次は ` impl ` の行です、これは他のトレイトを実装するときと同様です。
152
+ <!-- Next is the `impl` line, which is an implementing like any other trait. -->
153
+ 次は ` impl ` の行です。
154
+ これは他のトレイトを実装するときと同様です。
153
155
154
156
<!-- From here, we use `=` to define our associated types. The name the trait uses -->
155
157
<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
@@ -163,9 +165,9 @@ impl Graph for MyGraph {
163
165
## 関連型を伴うトレイト
164
166
165
167
<!-- 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
- もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合 :
168
+ <!-- try to create a trait object from a trait with an associated type, like this: -->
169
+ すこし触れておきたい構文のひとつに、トレイトオブジェクトがあります 。
170
+ もし、トレイトオブジェクトを以下のように関連型を持つトレイトから作成しようとした場合 :
169
171
170
172
``` rust,ignore
171
173
# trait Graph {
@@ -207,8 +209,9 @@ let obj = Box::new(graph) as Box<Graph>;
207
209
208
210
<!-- We can’t create a trait object like this, because we don’t know the associated -->
209
211
<!-- types. Instead, we can write this: -->
210
- 上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
211
- 代わりに以下のように書くことができます:
212
+ 上のようにしてトレイトオブジェクトを作ることはできません。
213
+ なぜなら関連型について知らないからです。
214
+ 代わりに以下のように書けます:
212
215
213
216
``` rust
214
217
# trait Graph {
@@ -237,5 +240,6 @@ let obj = Box::new(graph) as Box<Graph<N=Node, E=Edge>>;
237
240
<!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
238
241
<!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
239
242
<!-- couldn’t be sure which `impl` to match this trait object to. -->
240
- ` N=Node ` 構文を用いて型パラメータ ` N ` にたいして具体的な型 ` Node ` を指定することができます、` E=Edge ` についても同様です。
243
+ ` N=Node ` 構文を用いて型パラメータ ` N ` に対して具体的な型 ` Node ` を指定できます。
244
+ ` E=Edge ` についても同様です。
241
245
もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの ` impl ` がマッチするのか定まりません。
0 commit comments