77<!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
88<!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
99<!-- this: -->
10- 関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
11- 言い換えると、複数の型をグループ化するものです。
10+ 関連型は、Rust型システムの強力な部分です。
11+ 関連型は、「型族」という概念と関連があり、 言い換えると、複数の型をグループ化するものです。
1212この説明はすこし抽象的なので、実際の例を見ていきましょう。
1313例えば、 ` Graph ` トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
1414そのため、以下のように ` Graph<N, E> ` と書きたくなるでしょう:
@@ -25,20 +25,20 @@ trait Graph<N, E> {
2525<!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
2626<!-- the `N`ode and `E`dge types too: -->
2727たしかに上のようなコードは動作しますが、この ` Graph ` の定義は少し扱いづらいです。
28- たとえば、任意の ` Graph ` を引数に取る関数は、 _ 同時に _ 頂点 ` N ` と辺 ` E ` についてもジェネリックとなることになります :
28+ たとえば、任意の ` Graph ` を引数に取る関数は、 _ さらに _ 頂点 ` N ` と辺 ` E ` の型についてもジェネリックになる必要があります :
2929
3030``` rust,ignore
3131fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
3232```
3333
3434<!-- 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 ` に関連する部分は邪魔になります 。
3737
3838<!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
3939<!-- to form each kind of `Graph`. We can do that with associated types: -->
40- 本当に表現したいことは、それぞれのグラフ( ` Graph ` )は辺( ` E ` )や頂点( ` N ` )で構成されているということです 。
41- それは、以下のように関連型を用いて表現することができます :
40+ 本当に表現したいのは、それぞれの ` Graph ` は、辺 ` E ` と頂点 ` N ` で構成されていることです 。
41+ それは、以下のように関連型を用いて表現できます :
4242
4343``` rust
4444trait Graph {
@@ -52,15 +52,14 @@ trait Graph {
5252```
5353
5454<!-- Now, our clients can be abstract over a given `Graph`: -->
55- このようにすると、 ` Graph ` を使った関数は以下のように書くことができます :
55+ こうすると、使う側では、個々の ` Graph ` をより抽象的なものとして扱えます :
5656
5757``` rust,ignore
5858fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
5959```
6060
6161<!-- No need to deal with the `E`dge type here! -->
62- もう ` E ` について扱う必要はありません!
63-
62+ ここでは、頂点 ` E ` 型を扱わずに済んでいます!
6463
6564<!-- Let’s go over all this in more detail. -->
6665もっと詳しく見ていきましょう。
@@ -83,13 +82,13 @@ trait Graph {
8382
8483<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
8584<!-- of the trait, with the functions. -->
86- 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体や関数で利用します 。
85+ 非常にシンプルですね。関連型には ` type ` キーワードを使い、そしてトレイトの本体にある関数で利用します 。
8786
8887<!-- These `type` declarations can have all the same thing as functions do. For example, -->
8988<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
9089<!-- we could do this: -->
9190これらの ` type ` 宣言は、関数で利用できるものと同じものが全て利用できます。
92- たとえば、 ` N ` 型が ` Display ` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます :
91+ たとえば、頂点を表示するため ` N ` 型には ` Display ` を実装してほしいなら、以下のように指定できます :
9392
9493``` rust
9594use std :: fmt;
@@ -141,15 +140,18 @@ impl Graph for MyGraph {
141140<!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
142141<!-- gives you an idea of how to implement this kind of thing. We first need three -->
143142<!-- `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 -->
145144<!-- 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 ` を用います。
150151
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+ これは他のトレイトを実装するときと同様です。
153155
154156<!-- From here, we use `=` to define our associated types. The name the trait uses -->
155157<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
@@ -163,9 +165,9 @@ impl Graph for MyGraph {
163165## 関連型を伴うトレイト
164166
165167<!-- 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+ もし、トレイトオブジェクトを以下のように関連型を持つトレイトから作成しようとした場合 :
169171
170172``` rust,ignore
171173# trait Graph {
@@ -207,8 +209,9 @@ let obj = Box::new(graph) as Box<Graph>;
207209
208210<!-- We can’t create a trait object like this, because we don’t know the associated -->
209211<!-- types. Instead, we can write this: -->
210- 上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
211- 代わりに以下のように書くことができます:
212+ 上のようにしてトレイトオブジェクトを作ることはできません。
213+ なぜなら関連型について知らないからです。
214+ 代わりに以下のように書けます:
212215
213216``` rust
214217# trait Graph {
@@ -237,5 +240,6 @@ let obj = Box::new(graph) as Box<Graph<N=Node, E=Edge>>;
237240<!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
238241<!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
239242<!-- 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 ` についても同様です。
241245もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの ` impl ` がマッチするのか定まりません。
0 commit comments