]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0386.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0386.md
1 #### Note: this error code is no longer emitted by the compiler.
2
3 This error occurs when an attempt is made to mutate the target of a mutable
4 reference stored inside an immutable container.
5
6 For example, this can happen when storing a `&mut` inside an immutable `Box`:
7
8 ```
9 let mut x: i64 = 1;
10 let y: Box<_> = Box::new(&mut x);
11 **y = 2; // error, cannot assign to data in an immutable container
12 ```
13
14 This error can be fixed by making the container mutable:
15
16 ```
17 let mut x: i64 = 1;
18 let mut y: Box<_> = Box::new(&mut x);
19 **y = 2;
20 ```
21
22 It can also be fixed by using a type with interior mutability, such as `Cell`
23 or `RefCell`:
24
25 ```
26 use std::cell::Cell;
27
28 let x: i64 = 1;
29 let y: Box<Cell<_>> = Box::new(Cell::new(x));
30 y.set(2);
31 ```