1 A borrowed variable was used by a closure.
3 Erroneous code example:
6 fn you_know_nothing(jon_snow: &mut i32) {
7 let nights_watch = &jon_snow;
9 *jon_snow = 3; // error: closure requires unique access to `jon_snow`
10 // but it is already borrowed
12 println!("{}", nights_watch);
16 In here, `jon_snow` is already borrowed by the `nights_watch` reference, so it
17 cannot be borrowed by the `starks` closure at the same time. To fix this issue,
18 you can create the closure after the borrow has ended:
21 fn you_know_nothing(jon_snow: &mut i32) {
22 let nights_watch = &jon_snow;
23 println!("{}", nights_watch);
30 Or, if the type implements the `Clone` trait, you can clone it between
34 fn you_know_nothing(jon_snow: &mut i32) {
35 let mut jon_copy = jon_snow.clone();
39 println!("{}", jon_copy);