【Rust】运算符重载

环境

  • Rust 1.56.1
  • VSCode 1.61.2

概念

参考:https://doc.rust-lang.org/rust-by-example/trait/ops.html

示例

在 rust 中,运算符可以通过实现 trait 来进行重载,可以重载的运算符参考:https://doc.rust-lang.org/core/ops/

main.rs

use std::ops;

struct Foo;
struct Bar;

#[derive(Debug)]
struct FooBar;

#[derive(Debug)]
struct BarFoo;

// Add trait 可以重载加号
impl ops::Add<Bar> for Foo {
    type Output = FooBar;

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

impl ops::Add<Foo> for Bar {
    type Output = BarFoo;

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

        BarFoo
    }
}

fn main() {
    println!("Foo + Bar = {:?}", Foo + Bar);
    println!("Bar + Foo = {:?}", Bar + Foo);
}

总结

了解了 Rust 中的运算符的重载,可以对常见的运算符进行功能的重新定义。

附录

原文地址:https://www.cnblogs.com/jiangbo44/p/15724690.html