1 A variable which requires unique access is being used in more than one closure
4 Erroneous code example:
7 fn set(x: &mut isize) {
11 fn dragoooon(x: &mut isize) {
12 let mut c1 = || set(x);
13 let mut c2 = || set(x); // error!
20 To solve this issue, multiple solutions are available. First, is it required
21 for this variable to be used in more than one closure at a time? If it is the
22 case, use reference counted types such as `Rc` (or `Arc` if it runs
27 use std::cell::RefCell;
29 fn set(x: &mut isize) {
33 fn dragoooon(x: &mut isize) {
34 let x = Rc::new(RefCell::new(x));
35 let y = Rc::clone(&x);
36 let mut c1 = || { let mut x2 = x.borrow_mut(); set(&mut x2); };
37 let mut c2 = || { let mut x2 = y.borrow_mut(); set(&mut x2); }; // ok!
44 If not, just run closures one at a time:
47 fn set(x: &mut isize) {
51 fn dragoooon(x: &mut isize) {
52 { // This block isn't necessary since non-lexical lifetimes, it's just to
53 // make it more clear.
54 let mut c1 = || set(&mut *x);
56 } // `c1` has been dropped here so we're free to use `x` again!
57 let mut c2 = || set(&mut *x);