]> git.proxmox.com Git - rustc.git/blob - src/librustc_error_codes/error_codes/E0502.md
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_error_codes / error_codes / E0502.md
1 A variable already borrowed as immutable was borrowed as mutable.
2
3 Erroneous code example:
4
5 ```compile_fail,E0502
6 fn bar(x: &mut i32) {}
7 fn foo(a: &mut i32) {
8 let ref y = a; // a is borrowed as immutable.
9 bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
10 // as immutable
11 println!("{}", y);
12 }
13 ```
14
15 To fix this error, ensure that you don't have any other references to the
16 variable before trying to access it mutably:
17
18 ```
19 fn bar(x: &mut i32) {}
20 fn foo(a: &mut i32) {
21 bar(a);
22 let ref y = a; // ok!
23 println!("{}", y);
24 }
25 ```
26
27 For more information on Rust's ownership system, take a look at the
28 [References & Borrowing][references-and-borrowing] section of the Book.
29
30 [references-and-borrowing]: https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html