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 {:?}-side of balance immutably", index);
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 {:?}-side of balance mutably", index);
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
Implementations on Foreign Types
sourceimpl<I, T, const LANES: usize> IndexMut<I> for Simd<T, LANES> where
T: SimdElement,
I: SliceIndex<[T]>,
LaneCount<LANES>: SupportedLaneCount,
impl<I, T, const LANES: usize> IndexMut<I> for Simd<T, LANES> where
T: SimdElement,
I: SliceIndex<[T]>,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<I> IndexMut<I> for str where
I: SliceIndex<str>,
impl<I> IndexMut<I> for str where
I: SliceIndex<str>,
pub fn index_mut(&mut self, index: I) -> &mut <I as SliceIndex<str>>::Output
sourceimpl<T, O> IndexMut<Range<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<Range<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: Range<usize>
) -> &mut <BitSlice<T, O> as Index<Range<usize>>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
sourceimpl<T, O> IndexMut<RangeFrom<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<RangeFrom<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: RangeFrom<usize>
) -> &mut <BitSlice<T, O> as Index<RangeFrom<usize>>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
sourceimpl<A, O, Idx> IndexMut<Idx> for BitArray<A, O> where
A: BitViewSized,
O: BitOrder,
BitSlice<<A as BitView>::Store, O>: IndexMut<Idx>,
impl<A, O, Idx> IndexMut<Idx> for BitArray<A, O> where
A: BitViewSized,
O: BitOrder,
BitSlice<<A as BitView>::Store, O>: IndexMut<Idx>,
sourceimpl<T, O> IndexMut<RangeFull> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<RangeFull> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: RangeFull
) -> &mut <BitSlice<T, O> as Index<RangeFull>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
sourceimpl<T, O> IndexMut<RangeTo<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<RangeTo<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: RangeTo<usize>
) -> &mut <BitSlice<T, O> as Index<RangeTo<usize>>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
sourceimpl<T, O, Idx> IndexMut<Idx> for BitBox<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
impl<T, O, Idx> IndexMut<Idx> for BitBox<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
sourceimpl<T, O, Idx> IndexMut<Idx> for BitVec<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
impl<T, O, Idx> IndexMut<Idx> for BitVec<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: IndexMut<Idx>,
sourceimpl<T, O> IndexMut<RangeToInclusive<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<RangeToInclusive<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: RangeToInclusive<usize>
) -> &mut <BitSlice<T, O> as Index<RangeToInclusive<usize>>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
sourceimpl<T, O> IndexMut<RangeInclusive<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
impl<T, O> IndexMut<RangeInclusive<usize>> for BitSlice<T, O> where
O: BitOrder,
T: BitStore,
pub fn index_mut(
&mut self,
index: RangeInclusive<usize>
) -> &mut <BitSlice<T, O> as Index<RangeInclusive<usize>>>::OutputⓘNotable traits for &'_ BitSlice<T, O>impl<'_, T, O> Read for &'_ BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField, impl<'_, T, O> Write for &'_ mut BitSlice<T, O> where
T: BitStore,
O: BitOrder,
BitSlice<T, O>: BitField,