Skip to content

Commit 1b92cef

Browse files
committed
generate
1 parent 14e6348 commit 1b92cef

File tree

1 file changed

+130
-47
lines changed

1 file changed

+130
-47
lines changed

public/1.6/book/associated-types.html

Lines changed: 130 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta name="generator" content="rustdoc">
7-
<title>Associated Types</title>
7+
<title>関連型</title>
88

99
<link rel="stylesheet" type="text/css" href="rustbook.css">
1010

@@ -185,13 +185,26 @@
185185
<div id='page'>
186186

187187

188-
<h1 class="title">Associated Types</h1>
189-
<p>Associated types are a powerful part of Rust’s type system. They’re related to
190-
the idea of a ‘type family’, in other words, grouping multiple types together. That
191-
description is a bit abstract, so let’s dive right into an example. If you want
192-
to write a <code>Graph</code> trait, you have two types to be generic over: the node type
193-
and the edge type. So you might write a trait, <code>Graph&lt;N, E&gt;</code>, that looks like
194-
this:</p>
188+
<h1 class="title">関連型</h1>
189+
<!-- % Associated Types -->
190+
191+
<!-- Associated types are a powerful part of Rust’s type system. They’re related to -->
192+
193+
<!-- the idea of a ‘type family’, in other words, grouping multiple types together. That -->
194+
195+
<!-- description is a bit abstract, so let’s dive right into an example. If you want -->
196+
197+
<!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
198+
199+
<!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
200+
201+
<!-- this: -->
202+
203+
<p>関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
204+
言い換えると、複数の型をグループ化するものです。
205+
この説明はすこし抽象的なので、実際の例を見ていきましょう。
206+
例えば、 <code>Graph</code> トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
207+
そのため、以下のように <code>Graph&lt;N, E&gt;</code> と書きたくなるでしょう:</p>
195208
<span class='rusttest'>fn main() {
196209
trait Graph&lt;N, E&gt; {
197210
fn has_edge(&amp;self, &amp;N, &amp;N) -&gt; bool;
@@ -205,19 +218,31 @@ <h1 class="title">Associated Types</h1>
205218
<span class='comment'>// etc</span>
206219
}</pre>
207220

208-
<p>While this sort of works, it ends up being awkward. For example, any function
209-
that wants to take a <code>Graph</code> as a parameter now <em>also</em> needs to be generic over
210-
the <code>N</code>ode and <code>E</code>dge types too:</p>
221+
<!-- While this sort of works, it ends up being awkward. For example, any function -->
222+
223+
<!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
224+
225+
<!-- the `N`ode and `E`dge types too: -->
226+
227+
<p>たしかに上のようなコードは動作しますが、この <code>Graph</code> の定義は少し扱いづらいです。
228+
たとえば、任意の <code>Graph</code> を引数に取る関数は、 <em>同時に</em> 頂点 <code>N</code> と辺 <code>E</code> についてもジェネリックとなることになります:</p>
211229
<span class='rusttest'>fn main() {
212230
fn distance&lt;N, E, G: Graph&lt;N, E&gt;&gt;(graph: &amp;G, start: &amp;N, end: &amp;N) -&gt; u32 { ... }
213231
}</span><pre class='rust rust-example-rendered'>
214232
<span class='kw'>fn</span> <span class='ident'>distance</span><span class='op'>&lt;</span><span class='ident'>N</span>, <span class='ident'>E</span>, <span class='ident'>G</span>: <span class='ident'>Graph</span><span class='op'>&lt;</span><span class='ident'>N</span>, <span class='ident'>E</span><span class='op'>&gt;&gt;</span>(<span class='ident'>graph</span>: <span class='kw-2'>&amp;</span><span class='ident'>G</span>, <span class='ident'>start</span>: <span class='kw-2'>&amp;</span><span class='ident'>N</span>, <span class='ident'>end</span>: <span class='kw-2'>&amp;</span><span class='ident'>N</span>) <span class='op'>-&gt;</span> <span class='ident'>u32</span> { ... }</pre>
215233

216-
<p>Our distance calculation works regardless of our <code>Edge</code> type, so the <code>E</code> stuff in
217-
this signature is just a distraction.</p>
234+
<!-- Our distance calculation works regardless of our `Edge` type, so the `E` stuff in -->
235+
236+
<!-- this signature is just a distraction. -->
237+
238+
<p>この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる <code>E</code> に関連する部分は邪魔でしかありません。</p>
218239

219-
<p>What we really want to say is that a certain <code>E</code>dge and <code>N</code>ode type come together
220-
to form each kind of <code>Graph</code>. We can do that with associated types:</p>
240+
<!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
241+
242+
<!-- to form each kind of `Graph`. We can do that with associated types: -->
243+
244+
<p>本当に表現したいことは、それぞれのグラフ( <code>Graph</code> )は辺( <code>E</code> )や頂点( <code>N</code> )で構成されているということです。
245+
それは、以下のように関連型を用いて表現することができます:</p>
221246
<span class='rusttest'>fn main() {
222247
trait Graph {
223248
type N;
@@ -237,18 +262,28 @@ <h1 class="title">Associated Types</h1>
237262
<span class='comment'>// etc</span>
238263
}</pre>
239264

240-
<p>Now, our clients can be abstract over a given <code>Graph</code>:</p>
265+
<!-- Now, our clients can be abstract over a given `Graph`: -->
266+
267+
<p>このようにすると、<code>Graph</code> を使った関数は以下のように書くことができます:</p>
241268
<span class='rusttest'>fn main() {
242269
fn distance&lt;G: Graph&gt;(graph: &amp;G, start: &amp;G::N, end: &amp;G::N) -&gt; u32 { ... }
243270
}</span><pre class='rust rust-example-rendered'>
244271
<span class='kw'>fn</span> <span class='ident'>distance</span><span class='op'>&lt;</span><span class='ident'>G</span>: <span class='ident'>Graph</span><span class='op'>&gt;</span>(<span class='ident'>graph</span>: <span class='kw-2'>&amp;</span><span class='ident'>G</span>, <span class='ident'>start</span>: <span class='kw-2'>&amp;</span><span class='ident'>G</span>::<span class='ident'>N</span>, <span class='ident'>end</span>: <span class='kw-2'>&amp;</span><span class='ident'>G</span>::<span class='ident'>N</span>) <span class='op'>-&gt;</span> <span class='ident'>u32</span> { ... }</pre>
245272

246-
<p>No need to deal with the <code>E</code>dge type here!</p>
273+
<!-- No need to deal with the `E`dge type here! -->
274+
275+
<p>もう <code>E</code> について扱う必要はありません!</p>
276+
277+
<!-- Let’s go over all this in more detail. -->
278+
279+
<p>もっと詳しく見ていきましょう。</p>
247280

248-
<p>Let’s go over all this in more detail.</p>
281+
<!-- ## Defining associated types -->
249282

250-
<h2 id='defining-associated-types' class='section-header'><a href='#defining-associated-types'>Defining associated types</a></h2>
251-
<p>Let’s build that <code>Graph</code> trait. Here’s the definition:</p>
283+
<h2 id='関連型を定義する' class='section-header'><a href='#関連型を定義する'>関連型を定義する</a></h2>
284+
<!-- Let’s build that `Graph` trait. Here’s the definition: -->
285+
286+
<p>早速、<code>Graph</code> トレイトを定義しましょう。以下がその定義です:</p>
252287
<span class='rusttest'>fn main() {
253288
trait Graph {
254289
type N;
@@ -266,12 +301,20 @@ <h2 id='defining-associated-types' class='section-header'><a href='#defining-ass
266301
<span class='kw'>fn</span> <span class='ident'>edges</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='kw-2'>&amp;</span><span class='ident'>Self</span>::<span class='ident'>N</span>) <span class='op'>-&gt;</span> <span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>Self</span>::<span class='ident'>E</span><span class='op'>&gt;</span>;
267302
}</pre>
268303

269-
<p>Simple enough. Associated types use the <code>type</code> keyword, and go inside the body
270-
of the trait, with the functions.</p>
304+
<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
305+
306+
<!-- of the trait, with the functions. -->
307+
308+
<p>非常にシンプルですね。関連型には <code>type</code> キーワードを使い、そしてトレイトの本体や関数で利用します。</p>
309+
310+
<!-- These `type` declarations can have all the same thing as functions do. For example, -->
311+
312+
<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
271313

272-
<p>These <code>type</code> declarations can have all the same thing as functions do. For example,
273-
if we wanted our <code>N</code> type to implement <code>Display</code>, so we can print the nodes out,
274-
we could do this:</p>
314+
<!-- we could do this: -->
315+
316+
<p>これらの <code>type</code> 宣言は、関数で利用できるものと同じものが全て利用できます。
317+
たとえば、 <code>N</code> 型が <code>Display</code> を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます:</p>
275318
<span class='rusttest'>fn main() {
276319
use std::fmt;
277320

@@ -293,9 +336,15 @@ <h2 id='defining-associated-types' class='section-header'><a href='#defining-ass
293336
<span class='kw'>fn</span> <span class='ident'>edges</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='kw-2'>&amp;</span><span class='ident'>Self</span>::<span class='ident'>N</span>) <span class='op'>-&gt;</span> <span class='ident'>Vec</span><span class='op'>&lt;</span><span class='ident'>Self</span>::<span class='ident'>E</span><span class='op'>&gt;</span>;
294337
}</pre>
295338

296-
<h2 id='implementing-associated-types' class='section-header'><a href='#implementing-associated-types'>Implementing associated types</a></h2>
297-
<p>Just like any trait, traits that use associated types use the <code>impl</code> keyword to
298-
provide implementations. Here’s a simple implementation of Graph:</p>
339+
<!-- ## Implementing associated types -->
340+
341+
<h2 id='関連型を実装する' class='section-header'><a href='#関連型を実装する'>関連型を実装する</a></h2>
342+
<!-- Just like any trait, traits that use associated types use the `impl` keyword to -->
343+
344+
<!-- provide implementations. Here’s a simple implementation of Graph: -->
345+
346+
<p>通常のトレイトと同様に、関連型を使っているトレイトは実装するために <code>impl</code> を利用します。
347+
以下は、シンプルなGraphの実装例です:</p>
299348
<span class='rusttest'>fn main() {
300349
trait Graph {
301350
type N;
@@ -341,22 +390,45 @@ <h2 id='implementing-associated-types' class='section-header'><a href='#implemen
341390
}
342391
}</pre>
343392

344-
<p>This silly implementation always returns <code>true</code> and an empty <code>Vec&lt;Edge&gt;</code>, but it
345-
gives you an idea of how to implement this kind of thing. We first need three
346-
<code>struct</code>s, one for the graph, one for the node, and one for the edge. If it made
347-
more sense to use a different type, that would work as well, we’re just going to
348-
use <code>struct</code>s for all three here.</p>
393+
<!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
394+
395+
<!-- gives you an idea of how to implement this kind of thing. We first need three -->
396+
397+
<!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
398+
399+
<!-- more sense to use a different type, that would work as well, we’re just going to -->
400+
401+
<!-- use `struct`s for all three here. -->
402+
403+
<p>この奇妙な実装は、つねに <code>true</code> と空の <code>Vec&lt;Edge&gt;</code> を返しますますが、どのように定義したら良いかのアイデアをくれます。
404+
まず、はじめに3つの <code>struct</code> が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
405+
もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの <code>struct</code> を用います。</p>
406+
407+
<!-- Next is the `impl` line, which is just like implementing any other trait. -->
408+
409+
<p>次は <code>impl</code> の行です、これは他のトレイトを実装するときと同様です。</p>
410+
411+
<!-- From here, we use `=` to define our associated types. The name the trait uses -->
349412

350-
<p>Next is the <code>impl</code> line, which is just like implementing any other trait.</p>
413+
<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
351414

352-
<p>From here, we use <code>=</code> to define our associated types. The name the trait uses
353-
goes on the left of the <code>=</code>, and the concrete type we’re <code>impl</code>ementing this
354-
for goes on the right. Finally, we use the concrete types in our function
355-
declarations.</p>
415+
<!-- for goes on the right. Finally, we use the concrete types in our function -->
356416

357-
<h2 id='trait-objects-with-associated-types' class='section-header'><a href='#trait-objects-with-associated-types'>Trait objects with associated types</a></h2>
358-
<p>There’s one more bit of syntax we should talk about: trait objects. If you
359-
try to create a trait object from an associated type, like this:</p>
417+
<!-- declarations. -->
418+
419+
<p>そして、<code>=</code> を関連型を定義するために利用します。
420+
トレイトが利用する名前は <code>=</code> の左側にある名前で、実装に用いる具体的な型は右側にあるものになります。
421+
最後に、具体的な型を関数の宣言に利用します。</p>
422+
423+
<!-- ## Trait objects with associated types -->
424+
425+
<h2 id='関連型を伴うトレイト' class='section-header'><a href='#関連型を伴うトレイト'>関連型を伴うトレイト</a></h2>
426+
<!-- There’s one more bit of syntax we should talk about: trait objects. If you -->
427+
428+
<!-- try to create a trait object from an associated type, like this: -->
429+
430+
<p>すこし触れておきたい構文: トレイトオブジェクト が有ります。
431+
もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合:</p>
360432
<span class='rusttest'>fn main() {
361433
trait Graph {
362434
type N;
@@ -383,7 +455,9 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
383455
<span class='kw'>let</span> <span class='ident'>graph</span> <span class='op'>=</span> <span class='ident'>MyGraph</span>;
384456
<span class='kw'>let</span> <span class='ident'>obj</span> <span class='op'>=</span> <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>graph</span>) <span class='kw'>as</span> <span class='ident'>Box</span><span class='op'>&lt;</span><span class='ident'>Graph</span><span class='op'>&gt;</span>;</pre>
385457

386-
<p>You’ll get two errors:</p>
458+
<!-- You’ll get two errors: -->
459+
460+
<p>以下の様なエラーが発生します:</p>
387461

388462
<pre><code class="language-text">error: the value of the associated type `E` (from the trait `main::Graph`) must
389463
be specified [E0191]
@@ -395,8 +469,12 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
395469
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396470
</code></pre>
397471

398-
<p>We can’t create a trait object like this, because we don’t know the associated
399-
types. Instead, we can write this:</p>
472+
<!-- We can’t create a trait object like this, because we don’t know the associated -->
473+
474+
<!-- types. Instead, we can write this: -->
475+
476+
<p>上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
477+
代わりに以下のように書くことができます:</p>
400478
<span class='rusttest'>fn main() {
401479
trait Graph {
402480
type N;
@@ -423,9 +501,14 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
423501
<span class='kw'>let</span> <span class='ident'>graph</span> <span class='op'>=</span> <span class='ident'>MyGraph</span>;
424502
<span class='kw'>let</span> <span class='ident'>obj</span> <span class='op'>=</span> <span class='ident'>Box</span>::<span class='ident'>new</span>(<span class='ident'>graph</span>) <span class='kw'>as</span> <span class='ident'>Box</span><span class='op'>&lt;</span><span class='ident'>Graph</span><span class='op'>&lt;</span><span class='ident'>N</span><span class='op'>=</span><span class='ident'>Node</span>, <span class='ident'>E</span><span class='op'>=</span><span class='ident'>Edge</span><span class='op'>&gt;&gt;</span>;</pre>
425503

426-
<p>The <code>N=Node</code> syntax allows us to provide a concrete type, <code>Node</code>, for the <code>N</code>
427-
type parameter. Same with <code>E=Edge</code>. If we didn’t provide this constraint, we
428-
couldn’t be sure which <code>impl</code> to match this trait object to.</p>
504+
<!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
505+
506+
<!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
507+
508+
<!-- couldn’t be sure which `impl` to match this trait object to. -->
509+
510+
<p><code>N=Node</code> 構文を用いて型パラメータ <code>N</code> にたいして具体的な型 <code>Node</code> を指定することができます、<code>E=Edge</code> についても同様です。
511+
もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの <code>impl</code> がマッチするのか定まりません。</p>
429512

430513
<script type="text/javascript">
431514
window.playgroundUrl = "https://play.rust-lang.org";

0 commit comments

Comments
 (0)