比如在Rust中可以写
impl Mul for Vector {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
Self::new(self.x * rhs.x, self.y * rhs.y)
}
}
impl Mul<f32> for Vector {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Self::new(self.x * rhs, self.y * rhs)
}
}
同时支持f32
和Vector
的乘法。