In short, you cannot do this. The Index trait is defined as:
pub trait Index<Idx: ?Sized> {
type Output: ?Sized;
fn index(&self, index: Idx) -> &Self::Output;
}
That is, it takes a single argument of type Idx. The closest you can do is to use a tuple, which is a single type with multiple values packed into it:
impl std::ops::Index<(usize, usize)> for Matrix {
type Output = f32;
fn index(&self, idx: (usize, usize)) -> &f32 {
// or as appropriate for row- or column-major data
&self.data[idx.0 * self.cols + idx.1]
}
}
And it would be called like
matrix[(0, 1)]
bluss points out that the multiarray crate uses two-element arrays instead of a tuple. This is probably easier to type as you can just hit the square brackets twice:
impl std::ops::Index<[usize; 2]> for Matrix {
type Output = f32;
fn index(&self, idx: [usize; 2]) -> &f32 {
// or as appropriate for row- or column-major data
&self.data[idx[0] * self.cols + idx[1]]
}
}
And it's called like matrix[[0, 1]]. The important thing is that there's still just a single value provided as the argument to index.
Repeat the implementation as desired for Range, RangeTo, RangeFrom, and RangeFull. These are all single types, so you can call it like matrix[5..], for whatever that might mean.