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 the chapter
7 on [Capturing][capturing] in Rust By Example for more information.
9 [capturing]: https://doc.rust-lang.org/stable/rust-by-example/fn/closures/capture.html
11 Erroneous code example:
14 fn inside_closure(x: &mut i32) {
15 // Actions which require unique access
18 fn outside_closure(x: &mut i32) {
19 // Actions which require unique access
26 outside_closure(a); // error: cannot borrow `*a` as mutable because previous
27 // closure requires unique access.
32 To fix this error, you can finish using the closure before using the captured
36 fn inside_closure(x: &mut i32) {}
37 fn outside_closure(x: &mut i32) {}
44 // borrow on `a` ends.
45 outside_closure(a); // ok!
49 Or you can pass the variable as a parameter to the closure:
52 fn inside_closure(x: &mut i32) {}
53 fn outside_closure(x: &mut i32) {}
56 let mut bar = |s: &mut i32| {
64 It may be possible to define the closure later:
67 fn inside_closure(x: &mut i32) {}
68 fn outside_closure(x: &mut i32) {}