N-dimensional array in Rust I am practicing Rust by creating data structures, such as this N-dimensional array.
The purpose of this structure is to easily define and address into arbitrarily-deeply nested arrays without having to write out Vec&lt;Vec&lt;Vec&lt;T&gt;&gt;&gt; and incur the performance penalty coming from this (although performance is not a priority).
I am looking for feedback on how idiomatic the interface and implementation are,
as well as how to improve my tests since I don't think they're very robust.
use std::ops::{Index, IndexMut};

#[derive(Debug, Clone)]
pub struct NdArray&lt;T, const N: usize&gt; {
    dim: [usize; N],
    data: Vec&lt;T&gt;,
}

impl&lt;T, const N: usize&gt; NdArray&lt;T, N&gt; {
    fn calculate_capacity(dim: &amp;[usize; N]) -&gt; usize {
        let mut cap = 1;
        for &amp;x in dim {
            cap = usize::checked_mul(cap, x).expect(&quot;vector capacity overflowed usize&quot;);
        }
        cap
    }

    pub fn new(dim: [usize; N], default: T) -&gt; Self
    where
        T: Clone,
    {
        let cap = Self::calculate_capacity(&amp;dim);
        NdArray {
            dim,
            data: vec![default; cap],
        }
    }

    pub fn new_with(dim: [usize; N], generator: impl FnMut() -&gt; T) -&gt; Self {
        let cap = Self::calculate_capacity(&amp;dim);
        NdArray {
            dim,
            data: {
                let mut v = Vec::new();
                v.resize_with(cap, generator);
                v
            },
        }
    }

    fn get_flat_idx(&amp;self, idx: &amp;[usize; N]) -&gt; usize {
        let mut i = 0;
        for d in 0..self.dim.len() {
            assert!(
                idx[d] &lt; self.dim[d],
                &quot;index {} is out of bounds for dimension {} with size {}&quot;,
                idx[d],
                d,
                self.dim[d],
            );
            // This cannot overflow since we already checked product of all dimensions fits usize
            // and idx &lt; self.dim.
            i = i * self.dim[d] + idx[d];
        }
        i
    }
}

impl&lt;T, const N: usize&gt; Index&lt;&amp;[usize; N]&gt; for NdArray&lt;T, N&gt; {
    type Output = T;

    fn index(&amp;self, idx: &amp;[usize; N]) -&gt; &amp;T {
        &amp;self.data[self.get_flat_idx(idx)]
    }
}

impl&lt;T, const N: usize&gt; IndexMut&lt;&amp;[usize; N]&gt; for NdArray&lt;T, N&gt; {
    fn index_mut(&amp;mut self, idx: &amp;[usize; N]) -&gt; &amp;mut T {
        let i = self.get_flat_idx(idx);
        &amp;mut self.data[i]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::panic::catch_unwind;

    #[test]
    fn nd_array_1d() {
        let mut array = NdArray::new_with([10], || 1);
        assert_eq!(array[&amp;[0]], 1);
        assert_eq!(array[&amp;[9]], 1);
        assert!(catch_unwind(|| array[&amp;[10]]).is_err());

        array[&amp;[5]] = 99;
        assert_eq!(array[&amp;[4]], 1);
        assert_eq!(array[&amp;[5]], 99);
        assert_eq!(array[&amp;[6]], 1);
    }

    #[test]
    fn nd_array() {
        let mut array = NdArray::new([2, 3, 4], 0);
        array[&amp;[1, 2, 3]] = 10;
        assert_eq!(array[&amp;[1, 2, 3]], 10);
        assert_eq!(array[&amp;[1, 0, 0]], 0);
        assert!(catch_unwind(|| array[&amp;[2, 0, 0]]).is_err());
        assert_eq!(array[&amp;[0, 2, 0]], 0);
        assert!(catch_unwind(|| array[&amp;[0, 3, 0]]).is_err());
        assert_eq!(array[&amp;[0, 0, 3]], 0);
        assert!(catch_unwind(|| array[&amp;[0, 0, 4]]).is_err());
    }

    #[test]
    fn nd_array_overflow() {
        // Panics at the allocator code since sizeof(usize) &gt; 2
        // NdArray::new([usize::MAX / 2], 0);
        assert!(catch_unwind(|| NdArray::new([usize::MAX / 2 + 1, 2], 0)).is_err());
        assert!(catch_unwind(|| NdArray::new(
            [usize::MAX / 3, usize::MAX / 3, usize::MAX / 3 + 1],
            0
        ))
        .is_err());
    }
}
Nice example of const generics! In general your code is well organized and idiomatic. Some comments:

In your new() function, you are using an argument as a default value. It's more idiomatic to use the Default trait for this:
pub fn new(dim: [usize; N]) -&gt; Self
where
    T: Clone + Default,
{
    let cap = Self::calculate_capacity(&amp;dim);
    NdArray {
        dim,
        data: vec![Default::default(); cap],
    }
}

In some of your unit tests this means you have to use the turbofish to initialize: e.g. NdArray::new becomes NdArray::&lt;usize, 2&gt;::new. However, this only occurs when the user does not access / modify anything in the NdArray, and if they do that then type inference kicks in. So I recommend providing both options to the user, say NdArray::new(dim: ...) for T: Default and NdArray::new_fillwith(dim: ..., default: T) if the user wants to specify their own default value.

Your structure could use some more functionality. For any collection of T, .len() and .iter() would be the minimum:
pub fn len(&amp;self) -&gt; usize {
    self.data.len()
}

pub fn iter(&amp;self) -&gt; impl Iterator&lt;Item = &amp;T&gt; {
    self.data.iter()
}

What about adding two nd-arrays? Multiplying by a constant? Concatenating them along a certain dimension? Generalized matrix multiplication? Some of these will probably require certain utility functions.

For example the following would probably be quite useful and a good exercise to write: iteration along a sub-dimensional slice of the array, like pub fn iter_subarray(&amp;self, coords: &amp;[Option&lt;usize&gt;; N]) -&gt; impl Iterator&lt;Item = &amp;T&gt; where if the coord is None then you iterate along all coordinates in that dimension, but if it's Some(x) then that coordinate is fixed.


You probably want to #[derive(PartialEq, Eq)] in addition to Clone and Debug.

Both the implementation and unit tests could use some handling of edge cases, in particular when the dimension is empty and when any of the dimension lengths is zero. Whether you allow these cases is up to you; I would personally allow the former but not the latter. Here's a unit test:
#[test]
fn nd_array_edgecases() {
    let array_dim0 = NdArray::&lt;usize, 0&gt;::new([]);
    assert_eq!(array_dim0.len(), 1);
    assert_eq!(array_dim0[&amp;[]], 0);
    let array_dim1 = NdArray::&lt;usize, 1&gt;::new([0]);
    assert_eq!(array_dim1.len(), 0);
    assert!(catch_unwind(|| array_dim1[&amp;[0]]).is_err());
}


Great job with the unit tests for vector overflow, that is super useful.

In get_flat_idx, Clippy recommends: for (d, &amp;dlen) in self.dim.iter().enumerate() (then use dlen instead of self.dim[d]). I agree, though in this case we still have to use d to index into idx.

In your implementation, the field .data is not intended to dynamically change size. For this reason, you could initialize with Vec::with_capacity(n) instead of Vec::new() in your new_with() function. You might also consider using a fixed-size or fixed-capacity array implementation, for example the ArrayVec crate.