7

Let's try to compile this code:

use std::cell::RefCell;

struct Foo {
    v: Vec<RefCell<u8>>,
}

impl Foo {
    fn f(&self, i: usize) {
        let t = &mut *self.v[i].borrow_mut();
        //let t = &mut *{self.v[i].borrow_mut()}; //compiled ok
    }
}

fn main() {}

Compilation error:

error[E0596]: cannot borrow field `self.v` of immutable binding as mutable
 --> src/main.rs:9:23
  |
8 |     fn f(&self, i: usize) {
  |          ----- use `&mut self` here to make mutable
9 |         let t = &mut *self.v[i].borrow_mut();
  |                       ^^^^^^ cannot mutably borrow field of immutable binding

Why does this code require adding &mut self to function signature in order to compile?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
chabapok
  • 921
  • 1
  • 8
  • 14

2 Answers2

7

This is a known issue where IndexMut is sometimes selected when Index should actually be used.

Your workaround of using {} is reasonable, but you can also use Index explicitly:

use std::cell::RefCell;

fn f(v: Vec<RefCell<u8>>) {
    use std::ops::Index;
    let _t = &mut v.index(0).borrow_mut();
}

fn main() {}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
4

Another workaround is to explicitly call RefCell::borrow_mut(&v[0]).