Skip to content

Commit ca99e3d

Browse files
committed
generate
1 parent 72dfe72 commit ca99e3d

File tree

1 file changed

+111
-44
lines changed

1 file changed

+111
-44
lines changed

public/1.6/book/deref-coercions.html

Lines changed: 111 additions & 44 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>`Deref` coercions</title>
7+
<title>`Deref` による型強制</title>
88

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

@@ -185,9 +185,15 @@
185185
<div id='page'>
186186

187187

188-
<h1 class="title">`Deref` coercions</h1>
189-
<p>The standard library provides a special trait, <a href="../std/ops/trait.Deref.html"><code>Deref</code></a>. It’s normally
190-
used to overload <code>*</code>, the dereference operator:</p>
188+
<h1 class="title">`Deref` による型強制</h1>
189+
<!-- % `Deref` coercions -->
190+
191+
<!-- The standard library provides a special trait, [`Deref`][deref]. It’s normally -->
192+
193+
<!-- used to overload `*`, the dereference operator: -->
194+
195+
<p>標準ライブラリは特別なトレイト <a href="../std/ops/trait.Deref.html"><code>Dref</code></a> を提供します。
196+
<code>Deref</code> は通常、参照外し演算子 <code>*</code> をオーバーロードするために利用されます。</p>
191197
<span class='rusttest'>use std::ops::Deref;
192198

193199
struct DerefExample&lt;T&gt; {
@@ -226,95 +232,146 @@ <h1 class="title">`Deref` coercions</h1>
226232
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='string'>&#39;a&#39;</span>, <span class='op'>*</span><span class='ident'>x</span>);
227233
}</pre>
228234

229-
<p>This is useful for writing custom pointer types. However, there’s a language
230-
feature related to <code>Deref</code>: ‘deref coercions’. Here’s the rule: If you have a
231-
type <code>U</code>, and it implements <code>Deref&lt;Target=T&gt;</code>, values of <code>&amp;U</code> will
232-
automatically coerce to a <code>&amp;T</code>. Here’s an example:</p>
235+
<!-- This is useful for writing custom pointer types. However, there’s a language -->
236+
237+
<!-- feature related to `Deref`: ‘deref coercions’. Here’s the rule: If you have a -->
238+
239+
<!-- type `U`, and it implements `Deref<Target=T>`, values of `&U` will -->
240+
241+
<!-- automatically coerce to a `&T`. Here’s an example: -->
242+
243+
<p>このように、 <code>Deref</code> はカスタマイズしたポインタ型を定義するのに便利です。
244+
一方で、<code>Deref</code> に関連する機能がもう一つ有ります: 「derefによる型強制」です。
245+
これは、 <code>Deref&lt;Target=T&gt;</code> を実装している型 <code>U</code> があるときに、
246+
<code>&amp;U</code> が自動的に <code>&amp;T</code> に型強制されるというルールです。
247+
例えば:</p>
233248
<span class='rusttest'>fn main() {
234249
fn foo(s: &amp;str) {
235-
// borrow a string for a second
250+
// // borrow a string for a second
251+
// 一瞬だけ文字列を借用します
236252
}
237253

238-
// String implements Deref&lt;Target=str&gt;
254+
// // String implements Deref&lt;Target=str&gt;
255+
// String は Deref&lt;Target=str&gt; を実装しています
239256
let owned = &quot;Hello&quot;.to_string();
240257

241-
// therefore, this works:
258+
// // therefore, this works:
259+
// なので、以下のコードはきちんと動作します:
242260
foo(&amp;owned);
243261
}</span><pre class='rust rust-example-rendered'>
244262
<span class='kw'>fn</span> <span class='ident'>foo</span>(<span class='ident'>s</span>: <span class='kw-2'>&amp;</span><span class='ident'>str</span>) {
245-
<span class='comment'>// borrow a string for a second</span>
263+
<span class='comment'>// 一瞬だけ文字列を借用します</span>
246264
}
247265

248-
<span class='comment'>// String implements Deref&lt;Target=str&gt;</span>
266+
<span class='comment'>// String Deref&lt;Target=str&gt; を実装しています</span>
249267
<span class='kw'>let</span> <span class='ident'>owned</span> <span class='op'>=</span> <span class='string'>&quot;Hello&quot;</span>.<span class='ident'>to_string</span>();
250268

251-
<span class='comment'>// therefore, this works:</span>
269+
<span class='comment'>// なので、以下のコードはきちんと動作します:</span>
252270
<span class='ident'>foo</span>(<span class='kw-2'>&amp;</span><span class='ident'>owned</span>);</pre>
253271

254-
<p>Using an ampersand in front of a value takes a reference to it. So <code>owned</code> is a
255-
<code>String</code>, <code>&amp;owned</code> is an <code>&amp;String</code>, and since <code>impl Deref&lt;Target=str&gt; for String</code>, <code>&amp;String</code> will deref to <code>&amp;str</code>, which <code>foo()</code> takes.</p>
272+
<!-- Using an ampersand in front of a value takes a reference to it. So `owned` is a -->
256273

257-
<p>That’s it. This rule is one of the only places in which Rust does an automatic
258-
conversion for you, but it adds a lot of flexibility. For example, the <code>Rc&lt;T&gt;</code>
259-
type implements <code>Deref&lt;Target=T&gt;</code>, so this works:</p>
274+
<!-- `String`, `&owned` is an `&String`, and since `impl Deref<Target=str> for -->
275+
276+
<!-- String`, `&String` will deref to `&str`, which `foo()` takes. -->
277+
278+
<p>値の前にアンパサンド(&amp;)をつけることによってその値への参照を取得することができます。
279+
なので、 <code>owned</code><code>String</code> であり、 <code>&amp;owned</code><code>&amp;String</code> であり、
280+
そして、 <code>String</code><code>Deref&lt;Target=str&gt;</code> を実装しているために、
281+
<code>&amp;String</code><code>foo()</code> が要求している <code>&amp;str</code> に型強制されます。</p>
282+
283+
<!-- That’s it. This rule is one of the only places in which Rust does an automatic -->
284+
285+
<!-- conversion for you, but it adds a lot of flexibility. For example, the `Rc<T>` -->
286+
287+
<!-- type implements `Deref<Target=T>`, so this works: -->
288+
289+
<p>以上です! このルールはRustが自動的に変換を行う唯一の箇所の一つです。
290+
これによって、多くの柔軟性が手にはいります。
291+
例えば <code>Rc&lt;T&gt;</code><code>Deref&lt;Target=T&gt;</code> を実装しているため、以下のコードは正しく動作します:</p>
260292
<span class='rusttest'>fn main() {
261293
use std::rc::Rc;
262294

263295
fn foo(s: &amp;str) {
264-
// borrow a string for a second
296+
// // borrow a string for a second
297+
// 文字列を一瞬だけ借用します
265298
}
266299

267-
// String implements Deref&lt;Target=str&gt;
300+
// // String implements Deref&lt;Target=str&gt;
301+
// String は Deref&lt;Target=str&gt;を実装しています
268302
let owned = &quot;Hello&quot;.to_string();
269303
let counted = Rc::new(owned);
270304

271-
// therefore, this works:
305+
// // therefore, this works:
306+
// ゆえに、以下のコードは正しく動作します:
272307
foo(&amp;counted);
273308
}</span><pre class='rust rust-example-rendered'>
274309
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>rc</span>::<span class='ident'>Rc</span>;
275310

276311
<span class='kw'>fn</span> <span class='ident'>foo</span>(<span class='ident'>s</span>: <span class='kw-2'>&amp;</span><span class='ident'>str</span>) {
277-
<span class='comment'>// borrow a string for a second</span>
312+
<span class='comment'>// 文字列を一瞬だけ借用します</span>
278313
}
279314

280-
<span class='comment'>// String implements Deref&lt;Target=str&gt;</span>
315+
<span class='comment'>// String Deref&lt;Target=str&gt;を実装しています</span>
281316
<span class='kw'>let</span> <span class='ident'>owned</span> <span class='op'>=</span> <span class='string'>&quot;Hello&quot;</span>.<span class='ident'>to_string</span>();
282317
<span class='kw'>let</span> <span class='ident'>counted</span> <span class='op'>=</span> <span class='ident'>Rc</span>::<span class='ident'>new</span>(<span class='ident'>owned</span>);
283318

284-
<span class='comment'>// therefore, this works:</span>
319+
<span class='comment'>// ゆえに、以下のコードは正しく動作します:</span>
285320
<span class='ident'>foo</span>(<span class='kw-2'>&amp;</span><span class='ident'>counted</span>);</pre>
286321

287-
<p>All we’ve done is wrap our <code>String</code> in an <code>Rc&lt;T&gt;</code>. But we can now pass the
288-
<code>Rc&lt;String&gt;</code> around anywhere we’d have a <code>String</code>. The signature of <code>foo</code>
289-
didn’t change, but works just as well with either type. This example has two
290-
conversions: <code>Rc&lt;String&gt;</code> to <code>String</code> and then <code>String</code> to <code>&amp;str</code>. Rust will do
291-
this as many times as possible until the types match.</p>
322+
<!-- All we’ve done is wrap our `String` in an `Rc<T>`. But we can now pass the -->
323+
324+
<!-- `Rc<String>` around anywhere we’d have a `String`. The signature of `foo` -->
292325

293-
<p>Another very common implementation provided by the standard library is:</p>
326+
<!-- didn’t change, but works just as well with either type. This example has two -->
327+
328+
<!-- conversions: `Rc<String>` to `String` and then `String` to `&str`. Rust will do -->
329+
330+
<!-- this as many times as possible until the types match. -->
331+
332+
<p>先ほどのコードとの変化は <code>String</code><code>Rc&lt;T&gt;</code> でラッピングした点ですが、
333+
依然 <code>Rc&lt;String&gt;</code><code>String</code> が必要なところに渡すことができます。
334+
<code>foo</code> のシグネチャは変化していませんが、どちらの型についても正しく動作します。
335+
この例は2つの変換を含んでいます: <code>Rc&lt;String&gt;</code><code>String</code> に変換され、次に <code>String</code><code>&amp;str</code> に変換されます。
336+
Rustはこのような変換を型がマッチするまで必要なだけ繰り返します。</p>
337+
338+
<!-- Another very common implementation provided by the standard library is: -->
339+
340+
<p>標準ライブラリに頻繁に見られるその他の実装は例えば以下の様なものが有ります:</p>
294341
<span class='rusttest'>fn main() {
295342
fn foo(s: &amp;[i32]) {
296-
// borrow a slice for a second
343+
// // borrow a slice for a second
344+
// スライスを一瞬だけ借用します
297345
}
298346

299-
// Vec&lt;T&gt; implements Deref&lt;Target=[T]&gt;
347+
// // Vec&lt;T&gt; implements Deref&lt;Target=[T]&gt;
348+
// Vec&lt;T&gt; は Deref&lt;Target=[T]&gt; を実装しています
300349
let owned = vec![1, 2, 3];
301350

302351
foo(&amp;owned);
303352
}</span><pre class='rust rust-example-rendered'>
304353
<span class='kw'>fn</span> <span class='ident'>foo</span>(<span class='ident'>s</span>: <span class='kw-2'>&amp;</span>[<span class='ident'>i32</span>]) {
305-
<span class='comment'>// borrow a slice for a second</span>
354+
<span class='comment'>// スライスを一瞬だけ借用します</span>
306355
}
307356

308-
<span class='comment'>// Vec&lt;T&gt; implements Deref&lt;Target=[T]&gt;</span>
357+
<span class='comment'>// Vec&lt;T&gt; Deref&lt;Target=[T]&gt; を実装しています</span>
309358
<span class='kw'>let</span> <span class='ident'>owned</span> <span class='op'>=</span> <span class='macro'>vec</span><span class='macro'>!</span>[<span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>];
310359

311360
<span class='ident'>foo</span>(<span class='kw-2'>&amp;</span><span class='ident'>owned</span>);</pre>
312361

313-
<p>Vectors can <code>Deref</code> to a slice.</p>
362+
<!-- Vectors can `Deref` to a slice. -->
363+
364+
<p>ベクタはスライスに <code>Deref</code> することができます。</p>
365+
366+
<!-- ## Deref and method calls -->
314367

315-
<h2 id='deref-and-method-calls' class='section-header'><a href='#deref-and-method-calls'>Deref and method calls</a></h2>
316-
<p><code>Deref</code> will also kick in when calling a method. Consider the following
317-
example.</p>
368+
<h2 id='derefとメソッド呼び出し' class='section-header'><a href='#derefとメソッド呼び出し'>Derefとメソッド呼び出し</a></h2>
369+
<!-- `Deref` will also kick in when calling a method. Consider the following -->
370+
371+
<!-- example. -->
372+
373+
<p><code>Deref</code> はメソッド呼び出し時にも自動的に呼びだされます。
374+
例えば以下の様なコードを見てみましょう:</p>
318375
<span class='rusttest'>fn main() {
319376
struct Foo;
320377

@@ -336,8 +393,12 @@ <h2 id='deref-and-method-calls' class='section-header'><a href='#deref-and-metho
336393

337394
<span class='ident'>f</span>.<span class='ident'>foo</span>();</pre>
338395

339-
<p>Even though <code>f</code> is a <code>&amp;&amp;Foo</code> and <code>foo</code> takes <code>&amp;self</code>, this works. That’s
340-
because these things are the same:</p>
396+
<!-- Even though `f` is a `&&Foo` and `foo` takes `&self`, this works. That’s -->
397+
398+
<!-- because these things are the same: -->
399+
400+
<p><code>f</code><code>&amp;&amp;Foo</code> であり、 <code>foo</code><code>&amp;self</code> を引数に取るにも関わらずこのコードは動作します。
401+
これは、以下が全て等価なことによります:</p>
341402
<span class='rusttest'>fn main() {
342403
f.foo();
343404
(&amp;f).foo();
@@ -349,9 +410,15 @@ <h2 id='deref-and-method-calls' class='section-header'><a href='#deref-and-metho
349410
(<span class='op'>&amp;&amp;</span><span class='ident'>f</span>).<span class='ident'>foo</span>();
350411
(<span class='op'>&amp;&amp;</span><span class='op'>&amp;&amp;</span><span class='op'>&amp;&amp;</span><span class='op'>&amp;&amp;</span><span class='ident'>f</span>).<span class='ident'>foo</span>();</pre>
351412

352-
<p>A value of type <code>&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;Foo</code> can still have methods defined on <code>Foo</code>
353-
called, because the compiler will insert as many * operations as necessary to
354-
get it right. And since it’s inserting <code>*</code>s, that uses <code>Deref</code>.</p>
413+
<!-- A value of type `&&&&&&&&&&&&&&&&Foo` can still have methods defined on `Foo` -->
414+
415+
<!-- called, because the compiler will insert as many * operations as necessary to -->
416+
417+
<!-- get it right. And since it’s inserting `*`s, that uses `Deref`. -->
418+
419+
<p><code>&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;Foo</code> 型の値は <code>Foo</code> で定義されているメソッドを呼び出すことができます。
420+
これは、コンパイラが自動的に必要なだけ * 演算子を補うことによります。
421+
そして <code>*</code> が補われることによって <code>Deref</code> が利用される事になります。</p>
355422

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

0 commit comments

Comments
 (0)