Error code E0386
Note: this error code is no longer emitted by the compiler.
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
:
#![allow(unused)] fn main() { let mut x: i64 = 1; let y: Box<_> = Box::new(&mut x); **y = 2; // error, cannot assign to data in an immutable container }
This error can be fixed by making the container mutable:
#![allow(unused)] fn main() { let mut x: i64 = 1; let mut y: Box<_> = Box::new(&mut x); **y = 2; }
It can also be fixed by using a type with interior mutability, such as Cell
or RefCell
:
#![allow(unused)] fn main() { use std::cell::Cell; let x: i64 = 1; let y: Box<Cell<_>> = Box::new(Cell::new(x)); y.set(2); }