简体   繁体   中英

How can I use `index_mut` to get a mutable reference?

Even when I implement IndexMut for my struct, I cannot get a mutable reference to an element of structure inner vector.

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

struct Test<T> {
    data: Vec<T>,
}

impl<T> Index<usize> for Test<T> {
    type Output = T;
    fn index<'a>(&'a self, idx: usize) -> &'a T {
        return &self.data[idx];
    }
}

impl<T> IndexMut<usize> for Test<T> {
    fn index_mut<'a>(&'a mut self, idx: usize) -> &'a mut T {
        // even here I cannot get mutable reference to self.data[idx]
        return self.data.index_mut(idx);
    }
}

fn main() {
    let mut a: Test<i32> = Test { data: Vec::new() };
    a.data.push(1);
    a.data.push(2);
    a.data.push(3);
    let mut b = a[1];
    b = 10;

    // will print `[1, 2, 3]` instead of [1, 10, 3]
    println!("[{}, {}, {}]", a.data[0], a.data[1], a.data[2]);
}

How can I use index_mut to get a mutable reference? Is it possible?

You're almost there. Change this:

let mut b = a[1];
b = 10;

to this:

let b = &mut a[1];
*b = 10;

Indexing syntax returns the value itself, not a reference to it . Your code extracts one i32 from your vector and modifies the variable - naturally, it does not affect the vector itself. In order to obtain a reference through the index, you need to write it explicitly.

This is fairly natural: when you use indexing to access elements of a slice or an array, you get the values of the elements, not references to them, and in order to get a reference you need to write it explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM