What #[derive(IndexMut)] generates

Deriving IndexMut only works for structs with only a single field, e.g. newtypes. Furthermore it requires that the type also implements Index, so usually Index should also be derived. The result is that you will mutably index it's member directly.

1 Example usage

#[derive(Index, IndexMut)]
struct MyVec(Vec<i32>);

#[derive(Index, IndexMut)]
struct Numbers {
    numbers: Vec<i32>,
}

fn main() {
    let mut myvec = MyVec(vec![5, 8]);
    myvec[0] = 50;
    assert_eq!(50, myvec[0]);

    let mut numbers = Numbers{numbers: vec![100, 200]};
    numbers[1] = 400;
    assert_eq!(400, numbers[1]);
}

2 Tuple structs

When deriving IndexMut for a tuple struct with one field:

#[derive(Index, IndexMut)]
struct MyVec(Vec<i32>);

Code like this will be generated to implement IndexMut:

impl<__IdxT> ::std::ops::IndexMut<__IdxT> for MyVec
where
    Vec<i32>: ::std::ops::IndexMut<__IdxT>,
{
    #[inline]
    fn index_mut(&mut self, idx: __IdxT) -> &mut Self::Output {
        <Vec<i32> as ::std::ops::IndexMut<__IdxT>>::index_mut(&mut self.0, idx)
    }
}

3 Regular structs

When deriving IndexMut for a regular struct with one field:

#[derive(Index, IndexMut)]
struct Numbers {
    numbers: Vec<i32>,
}

Code like this will be generated to implement IndexMut:

impl<__IdxT> ::std::ops::IndexMut<__IdxT> for Numbers
where
    Vec<i32>: ::std::ops::IndexMut<__IdxT>,
{
    #[inline]
    fn index_mut(&mut self, idx: __IdxT) -> &mut Self::Output {
        <Vec<i32> as ::std::ops::IndexMut<__IdxT>>::index_mut(&mut self.numbers, idx)
    }
}

4 Enums

Deriving IndexMut is not supported for enums.