演算子
演算子のオーバーロードは、std::ops 内のトレイトを介して実装されます。
#[derive(Debug, Copy, Clone)]
struct Point {
x: i32,
y: i32,
}
impl std::ops::Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self { x: self.x + other.x, y: self.y + other.y }
}
}
fn main() {
let p1 = Point { x: 10, y: 20 };
let p2 = Point { x: 100, y: 200 };
println!("{p1:?} + {p2:?} = {:?}", p1 + p2);
}
This slide should take about 5 minutes.
議論のポイント:
&PointにAddを実装できます。これはどのような状況で役に立ちますか?- 回答:
Add:addはselfを使用します。演算子をオーバーロードする型TがCopyでない場合は、&Tの演算子もオーバーロードすることを検討する必要があります。これにより、呼び出し箇所での不要なクローン作成を回避できます。
- 回答:
Outputが関連型であるのはなぜですか?これをメソッドの型パラメータにできるでしょうか?- 短い回答: 関数型のパラメータは呼び出し元によって制御されますが、関連型(
Outputなど)はトレイトの実装者によって制御されます。
- 短い回答: 関数型のパラメータは呼び出し元によって制御されますが、関連型(
- 2 種類の型に対して
Addを実装できます。たとえば、impl Add<(i32, i32)> for PointはPointにタプルを追加します。
The Not trait (! operator) is notable because it does not “boolify” like the same operator in C-family languages; instead, for integer types it negates each bit of the number, which arithmetically is equivalent to subtracting it from -1: !5 == -6.