I am looking to implement 2D matrix functionality in Rust in a generic fashion where the elements of the matrix would be numerics (either i32, i64, u32, u64, f32, f64). The generic type would look something like shown below:
#[derive(Debug)]
pub struct Mat<T> {
data: Vec<T>,
shape: (usize, usize),
}
impl<T> Mat<T> where T: i32 OR i64 OR u32 OR u64 OR f32 OR f64{
pub fn new(){
...
}
}
I know that you can AND trait bounds with the + symbol in the form of "where T: bound1 + bound2". Is there a way to cleanly OR trait bounds together?
num
crate'snum::Num
trait (or the same thing fromnum_traits
to limit to just the traits, not the rest ofnum
) to just allow any numeric type if you want to support arbitrary numeric types. Or use the specificstd::ops
traits to specify which operations must be supported without specifically requiring something thatnum
defines the traits for (or for non-builtins, something that implements the traits fromnum
). – ShadowRangeri32
andi64
a types (not traits) and could also not be combined with+
to a trait bound. There is a way to implement a trait for a fixed list of types, but this is also not what you want. I would like to encourage you to look at the operations you need and just express them as types. E.g. the traits instd::ops
or thenum_traits
crate. – Markus Klein