I have code like this, but I can't figure out what the borrow checker wants me to do.
let mut optimum = None;
for parameter in problem_space.iter() {
let mut solution = Vec::with_capacity(solution_len);
let found = solve(&mut solution, parameter);
if !found { // further iteration won't improve the result
break;
} else if optimum == None || solution.len() < optimum.unwrap().len() {
optimum = Some(solution);
}
}
solution
is moved in the else if
branch, but I need to assign it to the optimum. Can I do this without cloning? Or maybe can I restructure my code?
I'm passing solution
as a mutable reference because the solve
function calls itself recursively and I want to reuse the buffer.