集册 Rust 语言中文版 操作符和重载

操作符和重载

欢马劈雪     最近更新时间:2020-08-04 05:37:59

110

Rust 允许有限形式的操作符重载。有一些操作符能够被重载。为了支持一个类型之间特定的操作符,有一个你可以实现的特定的特征,然后重载操作符。

例如,可以用 Add 特征重载 + 操作符。

use std::ops::Add;

#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}

impl Add for Point {
type Output = Point;

fn add(self, other: Point) -> Point {
Point { x: self.x + other.x, y: self.y + other.y }
}
}

fn main() {
let p1 = Point { x: 1, y: 0 };
let p2 = Point { x: 2, y: 3 };

let p3 = p1 + p2;

println!("{:?}", p3);
}

在 main 函数中,你可以在两个 Point之间使用 + 操作符,因为我们可以使用 Point 的方法 Add<Output=Point>

有许多操作符可以以这种方式被重载,所有的关联特征都在 std::ops 模块中。看看完整列表的文档。    

这些特征的实现遵循一个模式。让我们看看 Add 的更多细节:

pub trait Add<RHS = Self> {
type Output;

fn add(self, rhs: RHS) -> Self::Output;
}
展开阅读全文