]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0502.md
Auto merge of #66396 - smmalis37:pythontest, r=alexcrichton
[rust.git] / src / librustc_error_codes / error_codes / E0502.md
1 This error indicates that you are trying to borrow a variable as mutable when it
2 has already been borrowed as immutable.
3
4 Erroneous code example:
5
6 ```compile_fail,E0502
7 fn bar(x: &mut i32) {}
8 fn foo(a: &mut i32) {
9     let ref y = a; // a is borrowed as immutable.
10     bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
11             //        as immutable
12     println!("{}", y);
13 }
14 ```
15
16 To fix this error, ensure that you don't have any other references to the
17 variable before trying to access it mutably:
18
19 ```
20 fn bar(x: &mut i32) {}
21 fn foo(a: &mut i32) {
22     bar(a);
23     let ref y = a; // ok!
24     println!("{}", y);
25 }
26 ```
27
28 For more information on the rust ownership system, take a look at
29 https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html.