运算符重载
重载加法
use std::ops::Add;
#[derive(Debug)]
struct Complex {
a: f64,
b: f64,
}
impl Add for Complex {
type Output = Complex;
fn add(self, other: Complex) -> Complex {
Complex {a: self.a+other.a, b: self.b+other.b}
}
}
fn main() {
let cp1 = Complex{a: 1f64, b: 2.0};
let cp2 = Complex{a: 5.0, b:8.1};
let cp3 = cp1 + cp2;
print!("{:?}", cp3);
}神奇的Output以及动态分发
对范型的限制
Last updated