Trait core::ops::IndexMut1.0.0 [] [src]

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

The IndexMut trait is used to specify the functionality of indexing operations like container[index], when used in a mutable context.

Examples

A trivial implementation of IndexMut for a type Foo. When &mut Foo[2] happens, it ends up calling index_mut, and therefore, main prints Mutable indexing with 2!.

use std::ops::{Index, IndexMut}; #[derive(Copy, Clone)] struct Foo; impl Index<usize> for Foo { type Output = Foo; fn index(&self, _index: usize) -> &Foo { self } } impl IndexMut<usize> for Foo { fn index_mut(&mut self, index: usize) -> &mut Foo { println!("Mutable indexing with {}!", index); self } } fn main() { &mut Foo[2]; }
use std::ops::{Index, IndexMut};

#[derive(Copy, Clone)]
struct Foo;

impl Index<usize> for Foo {
    type Output = Foo;

    fn index(&self, _index: usize) -> &Foo {
        self
    }
}

impl IndexMut<usize> for Foo {
    fn index_mut(&mut self, index: usize) -> &mut Foo {
        println!("Mutable indexing with {}!", index);
        self
    }
}

fn main() {
    &mut Foo[2];
}Run

Required Methods

The method for the mutable indexing (container[index]) operation

Implementors