运算符重载

在 Rust 中,大部分运算符都可以通过 trait 来重载。也就是说,这些运算符可以根据它们输入的参数来完成不同的任务。为什么这样做是可行的呢,是因为运算符是对方法调用的语法糖。例如,a + b 中的 + 运算符会调用 add 方法(也就是 a.add(b))。这个 add 方法是 Add trait 的一部分。因此,+ 运算符可以被 Add trait 的实现者(implementor)使用。

点击这里查看列举的重载运算符 trait,比如 Add。(原文:A list of the traits, such as Add, that overload operators are available here.)

1. use std::ops;

3. struct Foo;
4. struct Bar;

6. #[derive(Debug)]
7. struct FooBar;

9. #[derive(Debug)]
10. struct BarFoo;

12. // `std::ops::Add` trait 在这里用来指明 `+` 的功能,我们给出 `Add<Bar>`——关于
13. // 加法的 trait,带有一个 `Bar` 类型的右操作数(RHS)。下面代码块实现了这样的
14. // 运算: Foo + Bar = FooBar。
15. impl ops::Add<Bar> for Foo {
16. type Output = FooBar;

18. fn add(self, _rhs: Bar) -> FooBar {
19. println!("> Foo.add(Bar) was called");

21. FooBar
22. }
23. }

25. // 通过反转类型,我们以实现非交换的加法作为结束。
26. // 这里我们给出 `Add<Foo>`——关于加法的 trait,带有一个 `Foo` 类型的右操作数。
27. // 这个代码块实现了这样的操作:Bar + Foo = BarFoo。
28. impl ops::Add<Foo> for Bar {
29. type Output = BarFoo;

31. fn add(self, _rhs: Foo) -> BarFoo {
32. println!("> Bar.add(Foo) was called");

34. BarFoo
35. }
36. }

38. fn main() {
39. println!("Foo + Bar = {:?}", Foo + Bar);
40. println!("Bar + Foo = {:?}", Bar + Foo);
41. }

参见:

Add, 语法索引