《铁 rust 》:
let int: i32 = 3;
let float: f32 = 3.3;
let res = int*float; // Invalid
let res = (int as f32)*float; // Valid
为了方便起见,我希望在*
操作符上实现覆盖,考虑到Rust的错误消息,这似乎是可能的:
cannot multiply `{integer}` by `{float}`
the trait `Mul<{float}>` is not implemented for `{integer}`
the following other types implement trait `Mul<Rhs>`:
但写impl Mul<i32> for f32
显然也是不可能的:
only traits defined in the current crate can be implemented for primitive types
define and implement a trait or new type instead
那么,这应该怎么做呢?有没有 crate 已经在实施这些?