1
- % Universal Function Call Syntax
1
+ % 共通の関数呼び出し構文
2
+ <!-- % Universal Function Call Syntax -->
2
3
3
- Sometimes, functions can have the same names. Consider this code:
4
+ <!-- Sometimes, functions can have the same names. Consider this code: -->
5
+ しばしば、同名の関数が存在する時があります。たとえば、以下のコードでは:
4
6
5
7
``` rust
6
8
trait Foo {
@@ -24,7 +26,8 @@ impl Bar for Baz {
24
26
let b = Baz ;
25
27
```
26
28
27
- If we were to try to call ` b.f() ` , we’d get an error:
29
+ <!-- If we were to try to call `b.f()`, we’d get an error: -->
30
+ もしここで、 ` b.f() ` を呼びだそうとすると、以下の様なエラーが発生します:
28
31
29
32
``` text
30
33
error: multiple applicable methods in scope [E0034]
@@ -41,8 +44,12 @@ note: candidate #2 is defined in an impl of the trait `main::Bar` for the type
41
44
42
45
```
43
46
44
- We need a way to disambiguate which method we need. This feature is called
45
- ‘universal function call syntax’, and it looks like this:
47
+ <!-- We need a way to disambiguate which method we need. This feature is called -->
48
+ <!-- ‘universal function call syntax’, and it looks like this: -->
49
+ このような場合は、どのメソッドを呼び出す必要があるのかについて曖昧性を排除する手段が必要です。
50
+ そのようなフィーチャーは 「共通の関数呼び出し構文」と呼ばれ、以下のように書けます:
51
+
52
+
46
53
47
54
``` rust
48
55
# trait Foo {
@@ -63,49 +70,60 @@ Foo::f(&b);
63
70
Bar :: f (& b );
64
71
```
65
72
66
- Let’s break it down.
73
+ <!-- Let’s break it down. -->
74
+ 部分的に見ていきましょう。
67
75
68
76
``` rust,ignore
69
77
Foo::
70
78
Bar::
71
79
```
72
80
73
- These halves of the invocation are the types of the two traits: ` Foo ` and
74
- ` Bar ` . This is what ends up actually doing the disambiguation between the two:
75
- Rust calls the one from the trait name you use.
81
+ <!-- These halves of the invocation are the types of the two traits: `Foo` and -->
82
+ <!-- `Bar`. This is what ends up actually doing the disambiguation between the two: -->
83
+ <!-- Rust calls the one from the trait name you use. -->
84
+ まず、呼び出しのこの部分は2つのトレイト ` Foo ` と ` Bar ` の型を表しています。
85
+ この部分が、実際にどちらのトレイトのメソッドを呼び出しているのかを指定し、曖昧性を排除している箇所になります。
76
86
77
87
``` rust,ignore
78
88
f(&b)
79
89
```
80
90
81
- When we call a method like ` b.f() ` using [ method syntax] [ methodsyntax ] , Rust
82
- will automatically borrow ` b ` if ` f() ` takes ` &self ` . In this case, Rust will
83
- not, and so we need to pass an explicit ` &b ` .
91
+ <!-- When we call a method like `b.f()` using [method syntax][methodsyntax], Rust -->
92
+ <!-- will automatically borrow `b` if `f()` takes `&self`. In this case, Rust will -->
93
+ <!-- not, and so we need to pass an explicit `&b`. -->
94
+ ` b.f() ` のように [ メソッド構文] [ methodsyntax ] を利用して呼び出した時、Rustは ` f() ` が ` &self ` を引数に取る場合自動的に ` b ` を借用します。
95
+ 今回の場合は、そのようには呼び出していないので、明示的に ` &b ` を渡してやる必要があります。
84
96
85
97
[ methodsyntax ] : method-syntax.html
86
98
87
- # Angle-bracket Form
99
+ # 山括弧形式
88
100
89
- The form of UFCS we just talked about:
101
+ <!-- The form of UFCS we just talked about: -->
102
+ すぐ上で説明した、以下のような共通の関数呼び出し構文:
90
103
91
104
``` rust,ignore
92
105
Trait::method(args);
93
106
```
94
107
95
- Is a short-hand. There’s an expanded form of this that’s needed in some
96
- situations:
108
+ <!-- Is a short-hand. There’s an expanded form of this that’s needed in some -->
109
+ <!-- situations: -->
110
+ これは短縮形であり、時々必要になる以下の様な展開された形式もあります:
97
111
98
112
``` rust,ignore
99
113
<Type as Trait>::method(args);
100
114
```
101
115
102
- The ` <>:: ` syntax is a means of providing a type hint. The type goes inside
103
- the ` <> ` s. In this case, the type is ` Type as Trait ` , indicating that we want
104
- ` Trait ` ’s version of ` method ` to be called here. The ` as Trait ` part is
105
- optional if it’s not ambiguous. Same with the angle brackets, hence the
106
- shorter form.
116
+ <!-- The `<>::` syntax is a means of providing a type hint. The type goes inside -->
117
+ <!-- the `<>`s. In this case, the type is `Type as Trait`, indicating that we want -->
118
+ <!-- `Trait`’s version of `method` to be called here. The `as Trait` part is -->
119
+ <!-- optional if it’s not ambiguous. Same with the angle brackets, hence the -->
120
+ <!-- shorter form. -->
121
+ ` <>:: ` という構文は型のヒントを意味しており、 ` <> ` のなかに型が入ります。
122
+ この場合、型は ` Type as Trait ` となり、 ` Trait ` のバージョンの ` method ` が呼ばれる事を期待していることを意味しています。
123
+ ` as Trait ` という部分は、曖昧でない場合は省略可能です。山括弧についても同様に省略可能であり、なので先程のさらに短い形になるのです。
107
124
108
- Here’s an example of using the longer form.
125
+ <!-- Here’s an example of using the longer form. -->
126
+ 長い形式を用いたサンプルコードは以下の通りです:
109
127
110
128
``` rust
111
129
trait Foo {
@@ -132,5 +150,6 @@ fn main() {
132
150
}
133
151
```
134
152
135
- Using the angle bracket syntax lets you call the trait method instead of the
136
- inherent one.
153
+ <!-- Using the angle bracket syntax lets you call the trait method instead of the -->
154
+ <!-- inherent one. -->
155
+ 山括弧構文を用いることでトレイトのメソッドを継承されたメソッドの代わりに呼び出すことができます。
0 commit comments