我想f64使用*运算符实现可交换的标量乘法运算.Mul<f64>为我的类型实现特征给了我一个右侧乘法.
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]
Run Code Online (Sandbox Code Playgroud)
对于非内置标量类型,我可以在标量上实现相同的特性,即Mul<Foo>在我的标量类型上实现.
如何获得内置类型的左侧实现f64?
你可以简单地恢复你的实现,通过交换f64与Foo
impl std::ops::Mul<Foo> for f64 {
type Output = Foo;
fn mul(self, rhs: Foo) -> Foo {
rhs * self
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |