How can I solve “use of moved value” and “which does not implement the `Copy` trait”?

The fix for this particular problem is to borrow the Vec you’re iterating over instead of moving it:

for element_index in &additions {
    let addition_aux = values[*element_index];
    addition = addition_aux + addition;
}

but your code has other problems. You never change additions by adding or removing elements, so your while additions.len() > 0 will never terminate. I hope this is because you haven’t finished and wanted to work out how to fix the immediate problem before writing the rest of the function.

For now, you might benefit from re-reading the chapter of the Rust Book about ownership, moves, and borrowing.

Leave a Comment