1
- % Operators and Overloading
1
+ % 演算子とオーバーロード
2
+ <!-- % Operators and Overloading -->
2
3
3
- Rust allows for a limited form of operator overloading. There are certain
4
- operators that are able to be overloaded. To support a particular operator
5
- between types, there’s a specific trait that you can implement, which then
6
- overloads the operator.
4
+ <!-- Rust allows for a limited form of operator overloading. There are certain -->
5
+ <!-- operators that are able to be overloaded. To support a particular operator -->
6
+ <!-- between types, there’s a specific trait that you can implement, which then -->
7
+ <!-- overloads the operator. -->
8
+ Rustは制限された形式での演算子オーバーロードを提供しており、オーバーロード可能な演算子がいくつか存在します。
9
+ 型同士の間の演算子をサポートするためのトレイトが存在し、それらを実装することで演算子をオーバーロードすることができます。
7
10
8
- For example, the ` + ` operator can be overloaded with the ` Add ` trait:
11
+ <!-- For example, the `+` operator can be overloaded with the `Add` trait: -->
12
+ 例えば、 ` + ` の演算子は ` Add ` トレイトを利用することでオーバーロードすることができます:
9
13
10
14
``` rust
11
15
use std :: ops :: Add ;
@@ -34,17 +38,23 @@ fn main() {
34
38
}
35
39
```
36
40
37
- In ` main ` , we can use ` + ` on our two ` Point ` s, since we’ve implemented
38
- ` Add<Output=Point> ` for ` Point ` .
41
+ <!-- In `main`, we can use `+` on our two `Point`s, since we’ve implemented -->
42
+ <!-- `Add<Output=Point>` for `Point`. -->
43
+ ` main ` 中で、2つの ` Point ` に対して ` + ` を使うことができます、
44
+ これは ` Point ` に対して ` Add<Output=Point> ` を実装したためです。
39
45
40
- There are a number of operators that can be overloaded this way, and all of
41
- their associated traits live in the [ ` std::ops ` ] [ stdops ] module. Check out its
42
- documentation for the full list.
46
+ <!-- There are a number of operators that can be overloaded this way, and all of -->
47
+ <!-- their associated traits live in the [`std::ops`][stdops] module. Check out its -->
48
+ <!-- documentation for the full list. -->
49
+ 同じ方法でオーバーロード可能な演算子が多数あります、
50
+ それらに対応したトレイトは [ ` std::ops ` ] [ stdops ] モジュール内に存在します。
51
+ 全てのオーバーロード可能な演算子と対応するトレイトについては [ ` std::ops ` ] [ stdops ] のドキュメントを読んで確認して下さい。
43
52
44
53
[ stdops ] : ../std/ops/index.html
45
54
46
- Implementing these traits follows a pattern. Let’s look at [ ` Add ` ] [ add ] in more
47
- detail:
55
+ <!-- Implementing these traits follows a pattern. Let’s look at [`Add`][add] in more -->
56
+ <!-- detail: -->
57
+ それらのトレイトの実装はパターンに従います。 [ ` Add ` ] [ add ] トレイトを詳しく見ていきましょう:
48
58
49
59
``` rust
50
60
# mod foo {
@@ -58,9 +68,11 @@ pub trait Add<RHS = Self> {
58
68
59
69
[ add ] : ../std/ops/trait.Add.html
60
70
61
- There’s three types in total involved here: the type you ` impl Add ` for, ` RHS ` ,
62
- which defaults to ` Self ` , and ` Output ` . For an expression ` let z = x + y ` , ` x `
63
- is the ` Self ` type, ` y ` is the RHS, and ` z ` is the ` Self::Output ` type.
71
+ <!-- There’s three types in total involved here: the type you `impl Add` for, `RHS`, -->
72
+ <!-- which defaults to `Self`, and `Output`. For an expression `let z = x + y`, `x` -->
73
+ <!-- is the `Self` type, `y` is the RHS, and `z` is the `Self::Output` type. -->
74
+ 関連する3つの型が存在します: ` impl Add ` を実装するもの、 デフォルトが ` Self ` の ` RHS ` 、 そして ` Output ` 。
75
+ 例えば、式 ` let z = x + y ` においては ` x ` は ` Self ` 型 ` y ` は RHS、 ` z ` は ` Self::Output ` 型となります。
64
76
65
77
``` rust
66
78
# struct Point ;
@@ -69,23 +81,28 @@ impl Add<i32> for Point {
69
81
type Output = f64 ;
70
82
71
83
fn add (self , rhs : i32 ) -> f64 {
72
- // add an i32 to a Point and get an f64
84
+ # // // add an i32 to a Point and get an f64
85
+ // i32をPointに加算しf64を返す
73
86
# 1.0
74
87
}
75
88
}
76
89
```
77
90
78
- will let you do this:
91
+ <!-- will let you do this: -->
92
+ 上のコードによって以下の様に書けるようになります:
79
93
80
94
``` rust,ignore
81
95
let p: Point = // ...
82
96
let x: f64 = p + 2i32;
83
97
```
84
98
85
- # Using operator traits in generic structs
99
+ <!-- # Using operator traits in generic structs -->
100
+ # オペレータトレイトをジェネリック構造体で使う
86
101
87
- Now that we know how operator traits are defined, we can define our ` HasArea `
88
- trait and ` Square ` struct from the [ traits chapter] [ traits ] more generically:
102
+ <!-- Now that we know how operator traits are defined, we can define our `HasArea` -->
103
+ <!-- trait and `Square` struct from the [traits chapter][traits] more generically: -->
104
+ オペレータトレイトがどのように定義されているかについて学びましたので、
105
+ [ トレイトについての章] [ traits ] の ` HasArea ` トレイトと ` Square ` 構造体をさらに一般的に定義することができます:
89
106
90
107
[ traits ] : traits.html
91
108
@@ -120,16 +137,22 @@ fn main() {
120
137
}
121
138
```
122
139
123
- For ` HasArea ` and ` Square ` , we just declare a type parameter ` T ` and replace
124
- ` f64 ` with it. The ` impl ` needs more involved modifications:
140
+ <!-- For `HasArea` and `Square`, we just declare a type parameter `T` and replace -->
141
+ <!-- `f64` with it. The `impl` needs more involved modifications: -->
142
+ ` HasArea ` と ` Square ` について、型パラメータ ` T ` を宣言し ` f64 ` で置換しました。
143
+ ` impl ` はさらに関連するモディフィケーションを必要とします:
125
144
126
145
``` ignore
127
146
impl<T> HasArea<T> for Square<T>
128
147
where T: Mul<Output=T> + Copy { ... }
129
148
```
130
149
131
- The ` area ` method requires that we can multiply the sides, so we declare that
132
- type ` T ` must implement ` std::ops::Mul ` . Like ` Add ` , mentioned above, ` Mul `
133
- itself takes an ` Output ` parameter: since we know that numbers don't change
134
- type when multiplied, we also set it to ` T ` . ` T ` must also support copying, so
135
- Rust doesn't try to move ` self.side ` into the return value.
150
+ <!-- The `area` method requires that we can multiply the sides, so we declare that -->
151
+ <!-- type `T` must implement `std::ops::Mul`. Like `Add`, mentioned above, `Mul` -->
152
+ <!-- itself takes an `Output` parameter: since we know that numbers don't change -->
153
+ <!-- type when multiplied, we also set it to `T`. `T` must also support copying, so -->
154
+ <!-- Rust doesn't try to move `self.side` into the return value. -->
155
+ ` area ` メソッドは辺を掛けることが可能なことを必要としています。
156
+ そのため型 ` T ` が ` std::ops::Mul ` を実装していなければならないと宣言しています。
157
+ 上で説明した ` Add ` と同様に、` Mul ` は ` Output ` パラメータを取ります: 数値を掛け算した時に型が変わらないことを知っていますので、 ` Output ` も ` T ` と設定します。
158
+ また ` T ` は、Rustが ` self.side ` を返り値にムーブするのを試みないようにコピーをサポートしている必要があります。
0 commit comments