I want to create some structs that have a property of a generic type T. This generic type will be used for calculations, so I want T to be all kind of numeric types such as i32, u32, f32, uf32, i64 etc. How can I achieve that?
This is what the num-traits crate can be used for. The Num
trait is implemented for all numeric types.
This ensures your generic type T
has all of the expected numeric operators, Add
, Sub
, Mul
, Div
, Rem
, can be partially equality checked via PartialEq
, it also exposes a value for 1
and 0
for T
.
You can see how the crate implements the trait here:
int_trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With