To solve this issue, multiple solutions are available. First, is it required
for this variable to be used in more than one closure at a time? If it is the
case, use reference counted types such as Rc (or Arc if it runs
concurrently):
#![allow(unused)]fnmain() {
use std::rc::Rc;
use std::cell::RefCell;
fnset(x: &mutisize) {
*x += 4;
}
fndragoooon(x: &mutisize) {
let x = Rc::new(RefCell::new(x));
let y = Rc::clone(&x);
letmut c1 = || { letmut x2 = x.borrow_mut(); set(&mut x2); };
letmut c2 = || { letmut x2 = y.borrow_mut(); set(&mut x2); }; // ok!
c2();
c1();
}
}
If not, just run closures one at a time:
#![allow(unused)]fnmain() {
fnset(x: &mutisize) {
*x += 4;
}
fndragoooon(x: &mutisize) {
{ // This block isn't necessary since non-lexical lifetimes, it's just to// make it more clear.letmut c1 = || set(&mut *x);
c1();
} // `c1` has been dropped here so we're free to use `x` again!letmut c2 = || set(&mut *x);
c2();
}
}