12

I can overload the [] operator with Index to return a ref, but I don't know if I have an overloaded operator to assign to the object.

This is what I want to do:

point[0] = 9.9;

This is what I can do so far (get a value):

use std::ops::Index;

#[derive(Debug, Clone, Copy)]
pub struct Vec3 {
    e: [f32; 3],
}

impl Index<usize> for Vec3 {
    type Output = f32;
    fn index<'a>(&'a self, i: usize) -> &'a f32 {
        &self.e[i]
    }
}

fn main() {
    let point = Vec3 { e: [0.0, 1.0, 3.0] };
    let z = point[2];
    println!("{}", z);
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
John Estess
  • 576
  • 10
  • 22

1 Answers1

15

You are using Index, which says this in its documentation:

If a mutable value is requested, IndexMut is used instead.

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

#[derive(Debug, Clone, Copy)]
pub struct Vec3 {
    e: [f32; 3],
}

impl Index<usize> for Vec3 {
    type Output = f32;
    fn index<'a>(&'a self, i: usize) -> &'a f32 {
        &self.e[i]
    }
}

impl IndexMut<usize> for Vec3 {
    fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut f32 {
        &mut self.e[i]
    }
}

fn main() {
    let mut point = Vec3 { e: [0.0, 1.0, 3.0] };
    point[0] = 99.9;
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • @JohnEstess that's why I linked to the extra question; it doesn't really have to do with `HashMap` (as I attempted to indicate in the parenthetical). Right now, there's no way to say `foo[index_not_in_collection] = new_value`. You can only do `foo[index_in_collection] = replacement_value`. Thus `IndexMut` returns a mutable reference that you then can use or replace, but it must already exist. – Shepmaster Apr 01 '18 at 01:40
  • 4
    @JohnEstess *the rather obtuse example* — Rust strives for good documentation. I'm sure that if you filed an issue explaining why the example threw you, someone would appreciate the feedback and might adjust the example accordingly. – Shepmaster Apr 01 '18 at 01:42
  • @Shepmaster not really true, you could add a new value, just modify the `index_mut` implementation to increase the size of the vector before returning. – edulix Sep 02 '19 at 05:24
  • @edulix not really. `DerefMut` needs to return a `&mut T`, and all references must be valid otherwise you create undefined behavior. You could increase the size of the vector, but you wouldn't have a valid value to store in that vector before returning a reference to it. Try implementing your suggestion to see the problem first-hand. ^_^ – Shepmaster Sep 09 '19 at 18:15