运算符重载

在 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;
  2. struct Foo;
  3. struct Bar;
  4. #[derive(Debug)]
  5. struct FooBar;
  6. #[derive(Debug)]
  7. struct BarFoo;
  8. // `std::ops::Add` trait 在这里用来指明 `+` 的功能,我们给出 `Add<Bar>`——关于
  9. // 加法的 trait,带有一个 `Bar` 类型的右操作数(RHS)。下面代码块实现了这样的
  10. // 运算: Foo + Bar = FooBar。
  11. impl ops::Add<Bar> for Foo {
  12. type Output = FooBar;
  13. fn add(self, _rhs: Bar) -> FooBar {
  14. println!("> Foo.add(Bar) was called");
  15. FooBar
  16. }
  17. }
  18. // 通过反转类型,我们以实现非交换的加法作为结束。
  19. // 这里我们给出 `Add<Foo>`——关于加法的 trait,带有一个 `Foo` 类型的右操作数。
  20. // 这个代码块实现了这样的操作:Bar + Foo = BarFoo。
  21. impl ops::Add<Foo> for Bar {
  22. type Output = BarFoo;
  23. fn add(self, _rhs: Foo) -> BarFoo {
  24. println!("> Bar.add(Foo) was called");
  25. BarFoo
  26. }
  27. }
  28. fn main() {
  29. println!("Foo + Bar = {:?}", Foo + Bar);
  30. println!("Bar + Foo = {:?}", Bar + Foo);
  31. }

参见:

Add, 语法索引