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?
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:
Another workaround is to explicitly call RefCell::borrow_mut(&v[0])
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With