我想用*
运算符实现一个交换标量f64
乘法运算.为我的类型实现Mul<f64>
trait会给我一个右侧乘法.
struct Foo(f64);
impl Mul<f64> for Foo {
type Output = Foo;
fn mul(self, _rhs: f64) -> Foo {
// implementation
}
}
let a = Foo(1.23);
a * 3.45; // works
3.45 * a; // error: the trait bound `{float}: std::ops::Mul<Foo>` is not satisfied [E0277]
对于非内置标量类型,我可以反过来在标量上实现相同的特性,即在我的标量类型上实现Mul<Foo>
.
如何为内置类型(如f64
)获得左侧实现?