This error occurs when an attempt is made to mutate the target of a mutable reference stored inside an immutable container.
For example, this can happen when storing a &mut inside an immutable Box:
let mut x: i64 = 1;
let y: Box<_> = Box::new(&mut x);
**y = 2; // error, cannot assign to data in an immutable containerRunThis error can be fixed by making the container mutable:
let mut x: i64 = 1;
let mut y: Box<_> = Box::new(&mut x);
**y = 2;RunIt can also be fixed by using a type with interior mutability, such as Cell
or RefCell:
use std::cell::Cell;
let x: i64 = 1;
let y: Box<Cell<_>> = Box::new(Cell::new(x));
y.set(2);Run