rust - How do I implement commutative (scalar) multiplication with a built-in type like `f64`? -
i'd implement commutative scalar f64 multiplication operation using * operator. implementing mul<f64> trait type gives me right side multiplication like.
struct foo(f64); impl mul<f64> foo { type output = foo; fn mul(self, _rhs: f64) -> foo { // implementation } } let = foo(1.23); * 3.45; // works 3.45 * a; // error: trait bound `{float}: std::ops::mul<foo>` not satisfied [e0277] for non-builtin scalar type, can implement same trait other way round on scalar, i.e. implementing mul<foo> on scalar type.
how left side implementation built-in type f64 too?
you can reverse implementation, swapping f64 foo
impl std::ops::mul<foo> f64 { type output = foo; fn mul(self, rhs: foo) -> foo { rhs * self } }
Comments
Post a Comment