chabapok
chabapok

Reputation: 952

Why can I not call borrow_mut() after indexing into an immutable Vec<RefCell>?

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?

Upvotes: 7

Views: 442

Answers (2)

Alexander Irbis
Alexander Irbis

Reputation: 113

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

Upvotes: 4

Shepmaster
Shepmaster

Reputation: 430506

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:

Upvotes: 7

Related Questions