Hi, I tried to write a recursive backtrack function. As compiler suggested, I put life time 'a all over the function. But compile still failed. Any suggestion? Thanks!
Original program is much longer, and I paste it on Playground.
struct LinesInfo {
}
struct Point {
}
struct Solution<'a> {
current: Vec<&'a Point>,
}
impl LinesInfo {
fn backtrack<'a>(&'a self, sol: &'a mut Solution<'a>) {
let candidates:Vec<&Point> = Vec::new();
for c in candidates {
sol.current.push(c);
self.backtrack(sol);
sol.current.pop();
}
}
}
fn main() {}
error[E0499]: cannot borrow `sol.current` as mutable more than once at a time
--> src\main.rs:15:13
|
12 | fn backtrack<'a>(&'a self, sol: &'a mut Solution<'a>) {
| -- lifetime `'a` defined here
...
15 | sol.current.push(c);
| ^^^^^^^^^^^ second mutable borrow occurs here
16 | self.backtrack(sol);
| -------------------
| | |
| | first mutable borrow occurs here
| argument requires that `*sol` is borrowed for `'a`
Other Two Errors
error[E0499]: cannot borrow `*sol` as mutable more than once at a time
--> src\main.rs:16:28
|
12 | fn backtrack<'a>(&'a self, sol: &'a mut Solution<'a>) {
| -- lifetime `'a` defined here
...
16 | self.backtrack(sol);
| ---------------^^^-
| | |
| | `*sol` was mutably borrowed here in the previous iteration of the loop
| argument requires that `*sol` is borrowed for `'a`
error[E0499]: cannot borrow `sol.current` as mutable more than once at a time
--> src\main.rs:17:13
|
12 | fn backtrack<'a>(&'a self, sol: &'a mut Solution<'a>) {
| -- lifetime `'a` defined here
...
16 | self.backtrack(sol);
| -------------------
| | |
| | first mutable borrow occurs here
| argument requires that `*sol` is borrowed for `'a`
17 | sol.current.pop();
| ^^^^^^^^^^^ second mutable borrow occurs here