@@ -309,15 +309,15 @@ <h3><a class="header" href="#独自のスマートポインタを定義する" i
309
309
references by default. Then we’ll look at how to add the ability to use the
310
310
dereference operator.
311
311
-->
312
- < p > 標準ライブラリが提供している< code > Box<T></ code > 型に似たスマートポインタを構築して、スマートポインタは既定で
313
- 参照に比べてどう異なって振る舞うのか経験しましょう 。それから、参照外し演算子を使う能力を追加する方法に目を向けましょう 。</ p >
312
+ < p > 標準ライブラリが提供している< code > Box<T></ code > 型に似たスマートポインタを作りましょう。そうすれば、スマートポインタがそのままだと
313
+ 参照と同じ様には振る舞わないことがわかります 。それから、どうすれば参照外し演算子を使えるようになるのか見てみましょう 。</ p >
314
314
<!--
315
315
The `Box<T>` type is ultimately defined as a tuple struct with one element, so
316
316
Listing 15-8 defines a `MyBox<T>` type in the same way. We’ll also define a
317
317
`new` function to match the `new` function defined on `Box<T>`.
318
318
-->
319
- < p > < code > Box<T></ code > 型は究極的に1要素のタプル構造体として定義されているので、リスト15-8は、同じように < code > MyBox<T></ code > 型を定義しています。
320
- また、< code > Box<T></ code > に定義された< code > new</ code > 関数と合致する < code > new</ code > 関数も定義しています。</ p >
319
+ < p > < code > Box<T></ code > 型は突き詰めると(訳註:データがヒープに置かれることを無視すると)1要素のタプル構造体のような定義になります。なのでリスト15-8ではそのように < code > MyBox<T></ code > 型を定義しています。
320
+ また、< code > Box<T></ code > に定義された< code > new</ code > 関数に対応する < code > new</ code > 関数も定義しています。</ p >
321
321
<!--
322
322
<span class="filename">Filename: src/main.rs</span>
323
323
-->
@@ -344,16 +344,16 @@ <h3><a class="header" href="#独自のスマートポインタを定義する" i
344
344
with one element of type `T`. The `MyBox::new` function takes one parameter of
345
345
type `T` and returns a `MyBox` instance that holds the value passed in.
346
346
-->
347
- < p > < code > MyBox</ code > という構造体を定義し、ジェネリック引数の< code > T</ code > を宣言しています。自分の型にどんな型の値も保持させたいからです 。
348
- < code > MyBox</ code > 型は、型 < code > T</ code > を1要素持つタプル構造体です 。< code > MyBox::new</ code > 関数は型< code > T</ code > の引数を1つ取り、
349
- 渡した値を保持する < code > MyBox</ code > インスタンスを返します 。</ p >
347
+ < p > < code > MyBox</ code > という構造体を定義し、ジェネリック引数の< code > T</ code > を宣言しています。この型にどんな型の値も持たせたいからです 。
348
+ < code > MyBox</ code > 型は型 < code > T</ code > の要素を1つ持つタプル構造体です 。< code > MyBox::new</ code > 関数は型< code > T</ code > の引数を1つ取り、
349
+ 渡した値を持つ < code > MyBox</ code > のインスタンスを返します 。</ p >
350
350
<!--
351
351
Let’s try adding the `main` function in Listing 15-7 to Listing 15-8 and
352
352
changing it to use the `MyBox<T>` type we’ve defined instead of `Box<T>`. The
353
353
code in Listing 15-9 won’t compile because Rust doesn’t know how to dereference
354
354
`MyBox`.
355
355
-->
356
- < p > 試しにリスト15-7の< code > main</ code > 関数をリスト15-8に追加し、< code > Box <T></ code > の代わりに定義した < code > MyBox <T></ code > 型を使うよう変更してみてください 。
356
+ < p > 試しにリスト15-7の< code > main</ code > 関数をリスト15-8に追加し、定義した < code > MyBox <T></ code > 型を < code > Box <T></ code > の代わりに使うよう変更してみてください 。
357
357
コンパイラは< code > MyBox</ code > を参照外しする方法がわからないので、リスト15-9のコードはコンパイルできません。</ p >
358
358
<!--
359
359
<span class="filename">Filename: src/main.rs</span>
@@ -375,7 +375,7 @@ <h3><a class="header" href="#独自のスマートポインタを定義する" i
375
375
<!--
376
376
Here’s the resulting compilation error:
377
377
-->
378
- < p > こちらが結果として出るコンパイルエラーです: </ p >
378
+ < p > こちらが結果として出るコンパイルエラーです。 </ p >
379
379
< pre > < code class ="language-text "> error[E0614]: type `MyBox<{integer}>` cannot be dereferenced
380
380
(エラー: 型`MyBox<{integer}>`は参照外しできません)
381
381
--> src/main.rs:14:19
@@ -388,7 +388,7 @@ <h3><a class="header" href="#独自のスマートポインタを定義する" i
388
388
ability on our type. To enable dereferencing with the `*` operator, we
389
389
implement the `Deref` trait.
390
390
-->
391
- < p > < code > MyBox<T></ code > に参照外しの能力を実装していないので、参照外しできません 。< code > *</ code > 演算子で参照外しできるようにするには、
391
+ < p > < code > MyBox<T></ code > の参照を外すことはできません。そのための実装を与えていないからです 。< code > *</ code > 演算子で参照外しできるようにするには、
392
392
< code > Deref</ code > トレイトを実装します。</ p >
393
393
<!--
394
394
### Treating a Type Like a Reference by Implementing the `Deref` Trait
@@ -401,10 +401,10 @@ <h3><a class="header" href="#derefトレイトを実装して型を参照のよ
401
401
borrows `self` and returns a reference to the inner data. Listing 15-10
402
402
contains an implementation of `Deref` to add to the definition of `MyBox`:
403
403
-->
404
- < p > 第10章で議論したように、トレイトを実装するには、トレイトの必須メソッドに実装を提供する必要があります 。
405
- < code > Deref</ code > トレイトは標準ライブラリで提供されていますが、 < code > self</ code > を借用し、
406
- 内部のデータへの参照を返す < code > deref </ code > という1つのメソッドを実装する必要があります。リスト15-10には、
407
- < code > MyBox</ code > の定義に追記する < code > Deref</ code > の実装が含まれています: </ p >
404
+ < p > 第10章で議論したように、トレイトを実装するにはトレイトの必須メソッドに実装を与える必要があります 。
405
+ < code > Deref</ code > トレイトは標準ライブラリで提供されており、 < code > deref </ code > という1つのメソッドの実装を要求します。 < code > deref </ code > は < code > self</ code > を借用し、
406
+ 内部のデータへの参照を返すメソッドです。
407
+ リスト15-10には、 < code > MyBox</ code > の定義に付け足す < code > Deref</ code > の実装が含まれています。 </ p >
408
408
<!--
409
409
<span class="filename">Filename: src/main.rs</span>
410
410
-->
@@ -434,44 +434,40 @@ <h3><a class="header" href="#derefトレイトを実装して型を参照のよ
434
434
parameter, but you don’t need to worry about them for now; we’ll cover them in
435
435
more detail in Chapter 19.
436
436
-->
437
- < p > < code > type Target = T;</ code > という記法は、< code > Deref</ code > トレイトが使用する関連型を定義しています。関連型は、
438
- ジェネリック引数を宣言する少しだけ異なる方法ですが、今は気にする必要はありません; 第19章でより詳しく講義します。</ p >
437
+ < p > < code > type Target = T;</ code > という記法は、< code > Deref</ code > トレイトが使用する関連型を定義しています。関連型はまた少し違ったやり方でジェネリック引数を宣言するためのものですが、今は気にする必要はありません。第19章でより詳しく扱います。</ p >
439
438
<!--
440
439
We fill in the body of the `deref` method with `&self.0` so `deref` returns a
441
440
reference to the value we want to access with the `*` operator. The `main`
442
441
function in Listing 15-9 that calls `*` on the `MyBox<T>` value now compiles,
443
442
and the assertions pass!
444
443
-->
445
- < p > < code > deref</ code > メソッドの本体を < code > &self.0</ code > で埋めているので 、< code > deref</ code > は < code > *</ code > 演算子でアクセスしたい値への参照を返します 。
444
+ < p > < code > deref</ code > メソッドの本体は < code > &self.0</ code > だけなので 、< code > deref</ code > が返すのは私達が < code > *</ code > 演算子でアクセスしたい値への参照なわけです 。
446
445
リスト15-9の< code > MyBox<T></ code > に< code > *</ code > を呼び出す< code > main</ code > 関数はこれでコンパイルでき、アサートも通ります!</ p >
447
446
<!--
448
447
Without the `Deref` trait, the compiler can only dereference `&` references.
449
448
The `deref` method gives the compiler the ability to take a value of any type
450
449
that implements `Deref` and call the `deref` method to get a `&` reference that
451
450
it knows how to dereference.
452
451
-->
453
- < p > < code > Deref</ code > がなければ 、コンパイラは< code > &</ code > 参照しか参照外しできなくなります。 < code > deref </ code > メソッドによりコンパイラは、
454
- < code > Deref</ code > を実装するあらゆる型の値を取り 、< code > deref</ code > メソッドを呼び出して参照外しの仕方を知っている < code > &</ code > 参照を得る能力を獲得するのです 。</ p >
452
+ < p > < code > Deref</ code > トレイトがないと 、コンパイラは< code > &</ code > 参照しか参照外しできません。
453
+ < code > deref </ code > メソッドのおかげで、コンパイラは < code > Deref</ code > を実装している型の値を取り 、< code > deref</ code > メソッドを呼ぶことで、参照外しが可能な < code > &</ code > 参照を得られるようになります 。</ p >
455
454
<!--
456
455
When we entered `*y` in Listing 15-9, behind the scenes Rust actually ran this
457
456
code:
458
457
-->
459
- < p > リスト15-9に< code > *y</ code > を入力した時、水面下でコンパイラは、実際にはこのようなコードを走らせていました: </ p >
458
+ < p > リスト15-9に< code > *y</ code > を入力した時、水面下でRustは実際にはこのようなコードを走らせていました。 </ p >
460
459
< pre > < code class ="language-rust ignore "> *(y.deref())
461
460
</ code > </ pre >
462
461
<!--
463
- 最後の行は、これで合っているのか自信がない・・・
464
- -->
465
- <!--
466
462
Rust substitutes the `*` operator with a call to the `deref` method and then a
467
463
plain dereference so we don’t have to think about whether or not we need to
468
464
call the `deref` method. This Rust feature lets us write code that functions
469
465
identically whether we have a regular reference or a type that implements
470
466
`Deref`.
471
467
-->
472
- < p > コンパイラは、 < code > *</ code > 演算子を< code > deref</ code > メソッド、それから何の変哲もない参照外しの呼び出しに置き換えるので 、
473
- < code > deref</ code > メソッドを呼び出す必要があるかどうかを考える必要はないわけです 。このRustの機能により、
474
- 普通の参照か< code > Deref</ code > を実装した型であるかどうかに関わらず、等しく機能するコードを書かせてくれます 。</ p >
468
+ < p > Rustが < code > *</ code > 演算子を< code > deref</ code > メソッドの呼び出しと普通の参照外しへと置き換えてくれるので 、
469
+ 私達は < code > deref</ code > メソッドを呼び出す必要があるかどうかを考えなくて済むわけです 。このRustの機能により、
470
+ 普通の参照か< code > Deref</ code > を実装した型であるかどうかに関わらず、等しく機能するコードを書くことができます 。</ p >
475
471
<!--
476
472
The reason the `deref` method returns a reference to a value and that the plain
477
473
dereference outside the parentheses in `*(y.deref())` is still necessary is the
@@ -480,21 +476,18 @@ <h3><a class="header" href="#derefトレイトを実装して型を参照のよ
480
476
to take ownership of the inner value inside `MyBox<T>` in this case or in most
481
477
cases where we use the dereference operator.
482
478
-->
483
- < p > < code > deref</ code > メソッドが値への参照を返し、< code > *(y.deref())</ code > のかっこの外の何の変哲もない参照外しがそれでも必要な理由は、
484
- 所有権システムです。< code > deref</ code > メソッドが値への参照ではなく、値を直接返したら、値は< code > self</ code > から外にムーブされてしまいます。
485
- 今回の場合や、参照外し演算子を使用する多くの場合には< code > MyBox<T></ code > の中の値の所有権を奪いたくはありません。</ p >
486
- <!--
487
- 2行目、... just once, [each time ...]という構造と思われる
488
- -->
479
+ < p > < code > deref</ code > メソッドが値への参照を返し、< code > *(y.deref())</ code > のかっこの外にある普通の参照外しがそれでも必要になるのは、
480
+ 所有権システムがあるからです。< code > deref</ code > メソッドが値への参照ではなく値を直接返したら、値は< code > self</ code > から外にムーブされてしまいます。
481
+ 今回もそうですが、参照外し演算子を使用するときはほとんどの場合、< code > MyBox<T></ code > の中の値の所有権を奪いたくはありません。</ p >
489
482
<!--
490
483
Note that the `*` operator is replaced with a call to the `deref` method and
491
484
then a call to `*` operator just once, each time we type a `*` in our code.
492
485
Because the substitution of the `*` operator does not recurse infinitely, we
493
486
end up with data of type `i32`, which matches the `5` in `assert_eq!` in
494
487
Listing 15-9.
495
488
-->
496
- < p > < code > *</ code > 演算子は、コードで < code > * </ code > を打つたびに、ただ1回、 < code > deref </ code > メソッドの呼び出し、そして < code > *</ code > 演算子の呼び出しに置き換えられることに注意してください 。
497
- < code > *</ code > 演算子の置き換えは、無限に繰り返されないので、 型< code > i32</ code > に行き着き、リスト15 -9で< code > assert_eq!</ code > の< code > 5</ code > と合致します。</ p >
489
+ < p > < code > *</ code > 演算子が < code > deref </ code > メソッドの呼び出しと < code > * </ code > 演算子の呼び出しに置き換えられるのは、コード内で < code > *</ code > を打つ毎にただ1回だけ、という点に注意して下さい 。
490
+ < code > *</ code > 演算子の置き換えは無限に繰り返されないので、 型< code > i32</ code > のデータに行き着きます。これはリスト15 -9で< code > assert_eq!</ code > の< code > 5</ code > と合致します。</ p >
498
491
<!--
499
492
### Implicit Deref Coercions with Functions and Methods
500
493
-->
0 commit comments