4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > `Deref` coercions </ title >
7
+ < title > `Deref` による型強制 </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
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 >
191
197
< span class ='rusttest '> use std::ops::Deref;
192
198
193
199
struct DerefExample<T> {
@@ -226,95 +232,146 @@ <h1 class="title">`Deref` coercions</h1>
226
232
< span class ='macro '> assert_eq</ span > < span class ='macro '> !</ span > (< span class ='string '> 'a'</ span > , < span class ='op '> *</ span > < span class ='ident '> x</ span > );
227
233
}</ pre >
228
234
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<Target=T></ code > , values of < code > &U</ code > will
232
- automatically coerce to a < code > &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<Target=T></ code > を実装している型 < code > U</ code > があるときに、
246
+ < code > &U</ code > が自動的に < code > &T</ code > に型強制されるというルールです。
247
+ 例えば:</ p >
233
248
< span class ='rusttest '> fn main() {
234
249
fn foo(s: &str) {
235
- // borrow a string for a second
250
+ // // borrow a string for a second
251
+ // 一瞬だけ文字列を借用します
236
252
}
237
253
238
- // String implements Deref<Target=str>
254
+ // // String implements Deref<Target=str>
255
+ // String は Deref<Target=str> を実装しています
239
256
let owned = "Hello".to_string();
240
257
241
- // therefore, this works:
258
+ // // therefore, this works:
259
+ // なので、以下のコードはきちんと動作します:
242
260
foo(&owned);
243
261
}</ span > < pre class ='rust rust-example-rendered '>
244
262
< span class ='kw '> fn</ span > < span class ='ident '> foo</ span > (< span class ='ident '> s</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> str</ span > ) {
245
- < span class ='comment '> // borrow a string for a second </ span >
263
+ < span class ='comment '> // 一瞬だけ文字列を借用します </ span >
246
264
}
247
265
248
- < span class ='comment '> // String implements Deref<Target=str></ span >
266
+ < span class ='comment '> // String は Deref<Target=str> を実装しています </ span >
249
267
< span class ='kw '> let</ span > < span class ='ident '> owned</ span > < span class ='op '> =</ span > < span class ='string '> "Hello"</ span > .< span class ='ident '> to_string</ span > ();
250
268
251
- < span class ='comment '> // therefore, this works :</ span >
269
+ < span class ='comment '> // なので、以下のコードはきちんと動作します :</ span >
252
270
< span class ='ident '> foo</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> owned</ span > );</ pre >
253
271
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 > &owned</ code > is an < code > &String</ code > , and since < code > impl Deref<Target=str> for String</ code > , < code > &String</ code > will deref to < code > &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 -->
256
273
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<T></ code >
259
- type implements < code > Deref<Target=T></ 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 > 値の前にアンパサンド(&)をつけることによってその値への参照を取得することができます。
279
+ なので、 < code > owned</ code > は < code > String</ code > であり、 < code > &owned</ code > は < code > &String</ code > であり、
280
+ そして、 < code > String</ code > が < code > Deref<Target=str></ code > を実装しているために、
281
+ < code > &String</ code > は < code > foo()</ code > が要求している < code > &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<T></ code > は < code > Deref<Target=T></ code > を実装しているため、以下のコードは正しく動作します:</ p >
260
292
< span class ='rusttest '> fn main() {
261
293
use std::rc::Rc;
262
294
263
295
fn foo(s: &str) {
264
- // borrow a string for a second
296
+ // // borrow a string for a second
297
+ // 文字列を一瞬だけ借用します
265
298
}
266
299
267
- // String implements Deref<Target=str>
300
+ // // String implements Deref<Target=str>
301
+ // String は Deref<Target=str>を実装しています
268
302
let owned = "Hello".to_string();
269
303
let counted = Rc::new(owned);
270
304
271
- // therefore, this works:
305
+ // // therefore, this works:
306
+ // ゆえに、以下のコードは正しく動作します:
272
307
foo(&counted);
273
308
}</ span > < pre class ='rust rust-example-rendered '>
274
309
< span class ='kw '> use</ span > < span class ='ident '> std</ span > ::< span class ='ident '> rc</ span > ::< span class ='ident '> Rc</ span > ;
275
310
276
311
< span class ='kw '> fn</ span > < span class ='ident '> foo</ span > (< span class ='ident '> s</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> str</ span > ) {
277
- < span class ='comment '> // borrow a string for a second </ span >
312
+ < span class ='comment '> // 文字列を一瞬だけ借用します </ span >
278
313
}
279
314
280
- < span class ='comment '> // String implements Deref<Target=str></ span >
315
+ < span class ='comment '> // String は Deref<Target=str>を実装しています </ span >
281
316
< span class ='kw '> let</ span > < span class ='ident '> owned</ span > < span class ='op '> =</ span > < span class ='string '> "Hello"</ span > .< span class ='ident '> to_string</ span > ();
282
317
< 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 > );
283
318
284
- < span class ='comment '> // therefore, this works :</ span >
319
+ < span class ='comment '> // ゆえに、以下のコードは正しく動作します :</ span >
285
320
< span class ='ident '> foo</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> counted</ span > );</ pre >
286
321
287
- < p > All we’ve done is wrap our < code > String</ code > in an < code > Rc<T></ code > . But we can now pass the
288
- < code > Rc<String></ 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<String></ code > to < code > String</ code > and then < code > String</ code > to < code > &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` -->
292
325
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<T></ code > でラッピングした点ですが、
333
+ 依然 < code > Rc<String></ code > を < code > String</ code > が必要なところに渡すことができます。
334
+ < code > foo</ code > のシグネチャは変化していませんが、どちらの型についても正しく動作します。
335
+ この例は2つの変換を含んでいます: < code > Rc<String></ code > が < code > String</ code > に変換され、次に < code > String</ code > が < code > &str</ code > に変換されます。
336
+ Rustはこのような変換を型がマッチするまで必要なだけ繰り返します。</ p >
337
+
338
+ <!-- Another very common implementation provided by the standard library is: -->
339
+
340
+ < p > 標準ライブラリに頻繁に見られるその他の実装は例えば以下の様なものが有ります:</ p >
294
341
< span class ='rusttest '> fn main() {
295
342
fn foo(s: &[i32]) {
296
- // borrow a slice for a second
343
+ // // borrow a slice for a second
344
+ // スライスを一瞬だけ借用します
297
345
}
298
346
299
- // Vec<T> implements Deref<Target=[T]>
347
+ // // Vec<T> implements Deref<Target=[T]>
348
+ // Vec<T> は Deref<Target=[T]> を実装しています
300
349
let owned = vec![1, 2, 3];
301
350
302
351
foo(&owned);
303
352
}</ span > < pre class ='rust rust-example-rendered '>
304
353
< span class ='kw '> fn</ span > < span class ='ident '> foo</ span > (< span class ='ident '> s</ span > : < span class ='kw-2 '> &</ span > [< span class ='ident '> i32</ span > ]) {
305
- < span class ='comment '> // borrow a slice for a second </ span >
354
+ < span class ='comment '> // スライスを一瞬だけ借用します </ span >
306
355
}
307
356
308
- < span class ='comment '> // Vec<T> implements Deref<Target=[T]></ span >
357
+ < span class ='comment '> // Vec<T> は Deref<Target=[T]> を実装しています </ span >
309
358
< 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 > ];
310
359
311
360
< span class ='ident '> foo</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> owned</ span > );</ pre >
312
361
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 -->
314
367
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 >
318
375
< span class ='rusttest '> fn main() {
319
376
struct Foo;
320
377
@@ -336,8 +393,12 @@ <h2 id='deref-and-method-calls' class='section-header'><a href='#deref-and-metho
336
393
337
394
< span class ='ident '> f</ span > .< span class ='ident '> foo</ span > ();</ pre >
338
395
339
- < p > Even though < code > f</ code > is a < code > &&Foo</ code > and < code > foo</ code > takes < code > &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 > &&Foo</ code > であり、 < code > foo</ code > は < code > &self</ code > を引数に取るにも関わらずこのコードは動作します。
401
+ これは、以下が全て等価なことによります:</ p >
341
402
< span class ='rusttest '> fn main() {
342
403
f.foo();
343
404
(&f).foo();
@@ -349,9 +410,15 @@ <h2 id='deref-and-method-calls' class='section-header'><a href='#deref-and-metho
349
410
(< span class ='op '> &&</ span > < span class ='ident '> f</ span > ).< span class ='ident '> foo</ span > ();
350
411
(< span class ='op '> &&</ span > < span class ='op '> &&</ span > < span class ='op '> &&</ span > < span class ='op '> &&</ span > < span class ='ident '> f</ span > ).< span class ='ident '> foo</ span > ();</ pre >
351
412
352
- < p > A value of type < code > &&&&&&&&&&&&&&&&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 > &&&&&&&&&&&&&&&&Foo</ code > 型の値は < code > Foo</ code > で定義されているメソッドを呼び出すことができます。
420
+ これは、コンパイラが自動的に必要なだけ * 演算子を補うことによります。
421
+ そして < code > *</ code > が補われることによって < code > Deref</ code > が利用される事になります。</ p >
355
422
356
423
< script type ="text/javascript ">
357
424
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments