A mutable variable is used but it is already captured by a closure.
Erroneous code example:
#![allow(unused)]fnmain() {
fninside_closure(x: &muti32) {
// Actions which require unique access
}
fnoutside_closure(x: &muti32) {
// Actions which require unique access
}
fnfoo(a: &muti32) {
letmut bar = || {
inside_closure(a)
};
outside_closure(a); // error: cannot borrow `*a` as mutable because previous// closure requires unique access.
bar();
}
}
ⓘ
This error indicates that a mutable variable is used while it is still captured
by a closure. Because the closure has borrowed the variable, it is not available
until the closure goes out of scope.
Note that a capture will either move or borrow a variable, but in this
situation, the closure is borrowing the variable. Take a look at the chapter
on Capturing in Rust By Example for more information.
To fix this error, you can finish using the closure before using the captured
variable: