pub trait IndexMut<Idx>: Index<Idx> where
    Idx: ?Sized
{ fn index_mut(&mut self, index: Idx) -> &mut Self::Output; }
Expand description

Used for indexing operations (container[index]) in mutable contexts.

container[index] is actually syntactic sugar for *container.index_mut(index), but only when used as a mutable value. If an immutable value is requested, the Index trait is used instead. This allows nice things such as v[index] = value.

Examples

A very simple implementation of a Balance struct that has two sides, where each can be indexed mutably and immutably.

use std::ops::{Index, IndexMut};

#[derive(Debug)]
enum Side {
    Left,
    Right,
}

#[derive(Debug, PartialEq)]
enum Weight {
    Kilogram(f32),
    Pound(f32),
}

struct Balance {
    pub left: Weight,
    pub right: Weight,
}

impl Index<Side> for Balance {
    type Output = Weight;

    fn index(&self, index: Side) -> &Self::Output {
        println!("Accessing {index:?}-side of balance immutably");
        match index {
            Side::Left => &self.left,
            Side::Right => &self.right,
        }
    }
}

impl IndexMut<Side> for Balance {
    fn index_mut(&mut self, index: Side) -> &mut Self::Output {
        println!("Accessing {index:?}-side of balance mutably");
        match index {
            Side::Left => &mut self.left,
            Side::Right => &mut self.right,
        }
    }
}

let mut balance = Balance {
    right: Weight::Kilogram(2.5),
    left: Weight::Pound(1.5),
};

// In this case, `balance[Side::Right]` is sugar for
// `*balance.index(Side::Right)`, since we are only *reading*
// `balance[Side::Right]`, not writing it.
assert_eq!(balance[Side::Right], Weight::Kilogram(2.5));

// However, in this case `balance[Side::Left]` is sugar for
// `*balance.index_mut(Side::Left)`, since we are writing
// `balance[Side::Left]`.
balance[Side::Left] = Weight::Kilogram(3.0);

Required Methods

Performs the mutable indexing (container[index]) operation.

Panics

May panic if the index is out of bounds.

Implementors

impl IndexMut<usize> for State

impl IndexMut<u8> for State

impl IndexMut<u32> for State

impl IndexMut<i32> for State

impl IndexMut<usize> for Block

impl IndexMut<u8> for Block

impl IndexMut<u32> for Block

impl IndexMut<i32> for Block

impl IndexMut<u8> for ChaChaIV

impl IndexMut<u8> for PolyKey

impl IndexMut<u32> for PolyKey

impl IndexMut<i32> for PolyKey

impl IndexMut<usize> for State

impl IndexMut<u8> for State

impl IndexMut<u32> for State

impl IndexMut<i32> for State

impl IndexMut<usize> for Row

impl IndexMut<u8> for Row

impl IndexMut<u32> for Row

impl IndexMut<i32> for Row

impl<'a, Q> IndexMut<&'a Q> for Map<String, Value> where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash

impl<I> IndexMut<I> for Value where
    I: Index

impl<T, P> IndexMut<usize> for Punctuated<T, P>

impl IndexMut<usize> for Block

impl IndexMut<u8> for Block

impl IndexMut<u32> for Block

impl IndexMut<i32> for Block

impl IndexMut<usize> for Word

impl IndexMut<u8> for Word

impl IndexMut<u32> for Word

impl IndexMut<i32> for Word

impl IndexMut<u8> for RoundKey

impl IndexMut<usize> for Nonce

impl IndexMut<u8> for Nonce

impl IndexMut<u32> for Nonce

impl IndexMut<i32> for Nonce

impl IndexMut<usize> for SBox

impl IndexMut<u8> for SBox

impl IndexMut<u32> for SBox

impl IndexMut<i32> for SBox

impl IndexMut<usize> for RCon

impl IndexMut<u8> for RCon

impl IndexMut<u32> for RCon

impl IndexMut<i32> for RCon

impl IndexMut<u8> for Bytes144

impl IndexMut<u8> for Bytes176

impl IndexMut<u8> for Bytes208

impl IndexMut<u8> for Bytes240

impl IndexMut<u8> for Key128

impl IndexMut<u32> for Key128

impl IndexMut<i32> for Key128

impl IndexMut<u8> for Key256

impl IndexMut<u32> for Key256

impl IndexMut<i32> for Key256

impl IndexMut<usize> for Block

impl IndexMut<u8> for Block

impl IndexMut<u32> for Block

impl IndexMut<i32> for Block

impl IndexMut<usize> for Key

impl IndexMut<u8> for Key

impl IndexMut<u32> for Key

impl IndexMut<i32> for Key

impl IndexMut<usize> for Tag

impl IndexMut<u8> for Tag

impl IndexMut<u32> for Tag

impl IndexMut<i32> for Tag

impl IndexMut<u8> for DigestB

impl IndexMut<u32> for DigestB

impl IndexMut<i32> for DigestB

impl IndexMut<usize> for Sigma

impl IndexMut<u8> for Sigma

impl IndexMut<u32> for Sigma

impl IndexMut<i32> for Sigma

impl<T: Numeric + Copy> IndexMut<usize> for State<T>

impl<T: Numeric + Copy> IndexMut<u8> for State<T>

impl<T: Numeric + Copy> IndexMut<u32> for State<T>

impl<T: Numeric + Copy> IndexMut<i32> for State<T>

impl<T: Numeric + Copy> IndexMut<usize> for DoubleState<T>

impl<T: Numeric + Copy> IndexMut<u8> for DoubleState<T>

impl<T: Numeric + Copy> IndexMut<u32> for DoubleState<T>

impl<T: Numeric + Copy> IndexMut<i32> for DoubleState<T>

impl<T: Numeric + Copy> IndexMut<usize> for Counter<T>

impl<T: Numeric + Copy> IndexMut<u8> for Counter<T>

impl<T: Numeric + Copy> IndexMut<u32> for Counter<T>

impl<T: Numeric + Copy> IndexMut<i32> for Counter<T>

impl IndexMut<usize> for Block

impl IndexMut<u8> for Block

impl IndexMut<u32> for Block

impl IndexMut<i32> for Block

impl IndexMut<u8> for Digest

impl IndexMut<u32> for Digest

impl IndexMut<i32> for Digest

impl IndexMut<usize> for Hash

impl IndexMut<u8> for Hash

impl IndexMut<u32> for Hash

impl IndexMut<i32> for Hash