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 > Universal Function Call Syntax </ title >
7
+ < title > 共通の関数呼び出し構文 </ 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 "> Universal Function Call Syntax</ h1 >
189
- < p > Sometimes, functions can have the same names. Consider this code:</ p >
188
+ < h1 class ="title "> 共通の関数呼び出し構文</ h1 >
189
+ <!-- % Universal Function Call Syntax -->
190
+
191
+ <!-- Sometimes, functions can have the same names. Consider this code: -->
192
+
193
+ < p > しばしば、同名の関数が存在する時があります。たとえば、以下のコードでは:</ p >
190
194
< span class ='rusttest '> fn main() {
191
195
trait Foo {
192
196
fn f(&self);
@@ -228,7 +232,9 @@ <h1 class="title">Universal Function Call Syntax</h1>
228
232
229
233
< span class ='kw '> let</ span > < span class ='ident '> b</ span > < span class ='op '> =</ span > < span class ='ident '> Baz</ span > ;</ pre >
230
234
231
- < p > If we were to try to call < code > b.f()</ code > , we’d get an error:</ p >
235
+ <!-- If we were to try to call `b.f()`, we’d get an error: -->
236
+
237
+ < p > もしここで、 < code > b.f()</ code > を呼びだそうとすると、以下の様なエラーが発生します:</ p >
232
238
233
239
< pre > < code class ="language-text "> error: multiple applicable methods in scope [E0034]
234
240
b.f();
@@ -244,8 +250,12 @@ <h1 class="title">Universal Function Call Syntax</h1>
244
250
245
251
</ code > </ pre >
246
252
247
- < p > We need a way to disambiguate which method we need. This feature is called
248
- ‘universal function call syntax’, and it looks like this:</ p >
253
+ <!-- We need a way to disambiguate which method we need. This feature is called -->
254
+
255
+ <!-- ‘universal function call syntax’, and it looks like this: -->
256
+
257
+ < p > このような場合は、どのメソッドを呼び出す必要があるのかについて曖昧性を排除する手段が必要です。
258
+ そのようなフィーチャーは 「共通の関数呼び出し構文」と呼ばれ、以下のように書けます:</ p >
249
259
< span class ='rusttest '> fn main() {
250
260
trait Foo {
251
261
fn f(&self);
@@ -267,47 +277,74 @@ <h1 class="title">Universal Function Call Syntax</h1>
267
277
< span class ='ident '> Foo</ span > ::< span class ='ident '> f</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> b</ span > );
268
278
< span class ='ident '> Bar</ span > ::< span class ='ident '> f</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> b</ span > );</ pre >
269
279
270
- < p > Let’s break it down.</ p >
280
+ <!-- Let’s break it down. -->
281
+
282
+ < p > 部分的に見ていきましょう。</ p >
271
283
< span class ='rusttest '> fn main() {
272
284
Foo::
273
285
Bar::
274
286
}</ span > < pre class ='rust rust-example-rendered '>
275
287
< span class ='ident '> Foo</ span > ::
276
288
< span class ='ident '> Bar</ span > ::</ pre >
277
289
278
- < p > These halves of the invocation are the types of the two traits: < code > Foo</ code > and
279
- < code > Bar</ code > . This is what ends up actually doing the disambiguation between the two:
280
- Rust calls the one from the trait name you use.</ p >
290
+ <!-- These halves of the invocation are the types of the two traits: `Foo` and -->
291
+
292
+ <!-- `Bar`. This is what ends up actually doing the disambiguation between the two: -->
293
+
294
+ <!-- Rust calls the one from the trait name you use. -->
295
+
296
+ < p > まず、呼び出しのこの部分は2つのトレイト < code > Foo</ code > と < code > Bar</ code > の型を表しています。
297
+ この部分が、実際にどちらのトレイトのメソッドを呼び出しているのかを指定し、曖昧性を排除している箇所になります。</ p >
281
298
< span class ='rusttest '> fn main() {
282
299
f(&b)
283
300
}</ span > < pre class ='rust rust-example-rendered '>
284
301
< span class ='ident '> f</ span > (< span class ='kw-2 '> &</ span > < span class ='ident '> b</ span > )</ pre >
285
302
286
- < p > When we call a method like < code > b.f()</ code > using < a href ="method-syntax.html "> method syntax</ a > , Rust
287
- will automatically borrow < code > b</ code > if < code > f()</ code > takes < code > &self</ code > . In this case, Rust will
288
- not, and so we need to pass an explicit < code > &b</ code > .</ p >
303
+ <!-- When we call a method like `b.f()` using [method syntax][methodsyntax], Rust -->
304
+
305
+ <!-- will automatically borrow `b` if `f()` takes `&self`. In this case, Rust will -->
306
+
307
+ <!-- not, and so we need to pass an explicit `&b`. -->
308
+
309
+ < p > < code > b.f()</ code > のように < a href ="method-syntax.html "> メソッド構文</ a > を利用して呼び出した時、Rustは < code > f()</ code > が < code > &self</ code > を引数に取る場合自動的に < code > b</ code > を借用します。
310
+ 今回の場合は、そのようには呼び出していないので、明示的に < code > &b</ code > を渡してやる必要があります。</ p >
289
311
290
- < h1 id ='angle-bracket-form ' class ='section-header '> < a href ='#angle-bracket-form '> Angle-bracket Form</ a > </ h1 >
291
- < p > The form of UFCS we just talked about:</ p >
312
+ < h1 id ='山括弧形式 ' class ='section-header '> < a href ='#山括弧形式 '> 山括弧形式</ a > </ h1 >
313
+ <!-- The form of UFCS we just talked about: -->
314
+
315
+ < p > すぐ上で説明した、以下のような共通の関数呼び出し構文:</ p >
292
316
< span class ='rusttest '> fn main() {
293
317
Trait::method(args);
294
318
}</ span > < pre class ='rust rust-example-rendered '>
295
319
< span class ='ident '> Trait</ span > ::< span class ='ident '> method</ span > (< span class ='ident '> args</ span > );</ pre >
296
320
297
- < p > Is a short-hand. There’s an expanded form of this that’s needed in some
298
- situations:</ p >
321
+ <!-- Is a short-hand. There’s an expanded form of this that’s needed in some -->
322
+
323
+ <!-- situations: -->
324
+
325
+ < p > これは短縮形であり、時々必要になる以下の様な展開された形式もあります:</ p >
299
326
< span class ='rusttest '> fn main() {
300
327
<Type as Trait>::method(args);
301
328
}</ span > < pre class ='rust rust-example-rendered '>
302
329
< span class ='op '> <</ span > < span class ='ident '> Type</ span > < span class ='kw '> as</ span > < span class ='ident '> Trait</ span > < span class ='op '> ></ span > ::< span class ='ident '> method</ span > (< span class ='ident '> args</ span > );</ pre >
303
330
304
- < p > The < code > <>::</ code > syntax is a means of providing a type hint. The type goes inside
305
- the < code > <></ code > s. In this case, the type is < code > Type as Trait</ code > , indicating that we want
306
- < code > Trait</ code > ’s version of < code > method</ code > to be called here. The < code > as Trait</ code > part is
307
- optional if it’s not ambiguous. Same with the angle brackets, hence the
308
- shorter form.</ p >
331
+ <!-- The `<>::` syntax is a means of providing a type hint. The type goes inside -->
332
+
333
+ <!-- the `<>`s. In this case, the type is `Type as Trait`, indicating that we want -->
334
+
335
+ <!-- `Trait`’s version of `method` to be called here. The `as Trait` part is -->
336
+
337
+ <!-- optional if it’s not ambiguous. Same with the angle brackets, hence the -->
309
338
310
- < p > Here’s an example of using the longer form.</ p >
339
+ <!-- shorter form. -->
340
+
341
+ < p > < code > <>::</ code > という構文は型のヒントを意味しており、 < code > <></ code > のなかに型が入ります。
342
+ この場合、型は < code > Type as Trait</ code > となり、 < code > Trait</ code > のバージョンの < code > method</ code > が呼ばれる事を期待していることを意味しています。
343
+ < code > as Trait</ code > という部分は、曖昧でない場合は省略可能です。山括弧についても同様に省略可能であり、なので先程のさらに短い形になるのです。</ p >
344
+
345
+ <!-- Here’s an example of using the longer form. -->
346
+
347
+ < p > 長い形式を用いたサンプルコードは以下の通りです:</ p >
311
348
< span class ='rusttest '> trait Foo {
312
349
fn foo() -> i32;
313
350
}
@@ -354,8 +391,11 @@ <h1 id='angle-bracket-form' class='section-header'><a href='#angle-bracket-form'
354
391
< span class ='macro '> assert_eq</ span > < span class ='macro '> !</ span > (< span class ='number '> 20</ span > , < span class ='ident '> Bar</ span > ::< span class ='ident '> foo</ span > ());
355
392
}</ pre >
356
393
357
- < p > Using the angle bracket syntax lets you call the trait method instead of the
358
- inherent one.</ p >
394
+ <!-- Using the angle bracket syntax lets you call the trait method instead of the -->
395
+
396
+ <!-- inherent one. -->
397
+
398
+ < p > 山括弧構文を用いることでトレイトのメソッドを継承されたメソッドの代わりに呼び出すことができます。</ p >
359
399
360
400
< script type ="text/javascript ">
361
401
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments