1 This error indicates that a mutable variable is being used while it is still
2 captured by a closure. Because the closure has borrowed the variable, it is not
3 available for use until the closure goes out of scope.
5 Note that a capture will either move or borrow a variable, but in this
6 situation, the closure is borrowing the variable. Take a look at
7 http://rustbyexample.com/fn/closures/capture.html for more information about
10 Erroneous code example:
13 fn inside_closure(x: &mut i32) {
14 // Actions which require unique access
17 fn outside_closure(x: &mut i32) {
18 // Actions which require unique access
25 outside_closure(a); // error: cannot borrow `*a` as mutable because previous
26 // closure requires unique access.
31 To fix this error, you can finish using the closure before using the captured
35 fn inside_closure(x: &mut i32) {}
36 fn outside_closure(x: &mut i32) {}
43 // borrow on `a` ends.
44 outside_closure(a); // ok!
48 Or you can pass the variable as a parameter to the closure:
51 fn inside_closure(x: &mut i32) {}
52 fn outside_closure(x: &mut i32) {}
55 let mut bar = |s: &mut i32| {
63 It may be possible to define the closure later:
66 fn inside_closure(x: &mut i32) {}
67 fn outside_closure(x: &mut i32) {}