The check_vec()
closure captures a mutable reference to the values
vector. The compiler is not smart enough to understand that values.push(0xbeef)
call is safe. I have tried compiling using NLL feature flag, but that didn't help either.
fn main() {
let mut values = Vec::with_capacity(16);
let mut check_vec = || {
if values.len() < 16 {
values.push(0xdead);
}
};
for _ in 0..32 {
check_vec();
values.push(0xbeef);
}
check_vec();
}
error[E0499]: cannot borrow `values` as mutable more than once at a time
--> src/main.rs:12:9
|
4 | let mut check_vec = || {
| -- first mutable borrow occurs here
5 | if values.len() < 16 {
| ------ previous borrow occurs due to use of `values` in closure
...
12 | values.push(0xbeef);
| ^^^^^^ second mutable borrow occurs here
...
16 | }
| - first borrow ends here
Are there any ways to mitigate or workaround this limitation?