@@ -25,7 +25,7 @@ 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 { ... }
@@ -37,7 +37,7 @@ fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
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 ` )で構成されているということです 。
40
+ 本当に表現したいのは、それぞれの ` Graph ` は、辺 ` E ` と頂点 ` N ` で構成されていることです 。
41
41
それは、以下のように関連型を用いて表現できます:
42
42
43
43
``` rust
@@ -52,14 +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 ` について扱う必要はありません !
62
+ ここでは、頂点 ` E ` 型を扱わずに済んでいます !
63
63
64
64
<!-- Let’s go over all this in more detail. -->
65
65
もっと詳しく見ていきましょう。
@@ -82,13 +82,13 @@ trait Graph {
82
82
83
83
<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
84
84
<!-- of the trait, with the functions. -->
85
- 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体や関数で利用します 。
85
+ 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体にある関数で利用します 。
86
86
87
87
<!-- These `type` declarations can have all the same thing as functions do. For example, -->
88
88
<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
89
89
<!-- we could do this: -->
90
90
これらの ` type ` 宣言は、関数で利用できるものと同じものが全て利用できます。
91
- たとえば、 ` N ` 型が ` Display ` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定できます :
91
+ たとえば、頂点を表示するために、 ` N ` 型に ` Display ` を実装してほしい場合は、以下のように指定できます :
92
92
93
93
``` rust
94
94
use std :: fmt;
@@ -142,13 +142,15 @@ impl Graph for MyGraph {
142
142
<!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
143
143
<!-- more sense to use a different type, that would work as well, we’re going to -->
144
144
<!-- use `struct`s for all three here. -->
145
- この奇妙な実装は、常に ` true ` と空の ` Vec<Edge> ` を返しますが、どのように定義したら良いかのアイデアをくれます。
146
- まず、はじめに3つの ` struct ` が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
147
- もし異なる型を利用することが適切ならば、そのようにすると良いでしょう。
145
+ この少し単純すぎる実装は、常に ` true ` と空の ` Vec<Edge> ` を返しますが、どのように定義したら良いかのアイデアをくれます。
146
+ まず、はじめに3つの ` struct ` が必要です。
147
+ グラフのためにひとつ、頂点のためにひとつ、辺のためにひとつです。
148
+ もし異なる型を利用するのが適切ならば、そうしても構いません。
148
149
今回はこの3つの ` struct ` を用います。
149
150
150
151
<!-- Next is the `impl` line, which is an implementing like any other trait. -->
151
- 次は ` impl ` の行です、これは他のトレイトを実装するときと同様です。
152
+ 次は ` impl ` の行です。
153
+ これは他のトレイトを実装するときと同様です。
152
154
153
155
<!-- From here, we use `=` to define our associated types. The name the trait uses -->
154
156
<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
@@ -208,7 +210,7 @@ let obj = Box::new(graph) as Box<Graph>;
208
210
<!-- types. Instead, we can write this: -->
209
211
上のようにしてトレイトオブジェクトを作ることはできません。
210
212
なぜなら関連型について知らないからです。
211
- 代わりに以下のように書くことができます :
213
+ 代わりに以下のように書けます :
212
214
213
215
``` rust
214
216
# trait Graph {
0 commit comments