Help to fix build error "use of moved value"

First things first, the hint it gives you is that you're missing the ability to copy:

   = note: move occurs because `commits` has type `std::vec::Vec<u8>`, which does not implement the `Copy` trait

The hint in itself is pretty simple, but kind of subtle if you're coming from languages where it would happen anyway (usually without your express consent). As you're likely new to Rust, it's worth mentioning that the Rust compiler is very good at telling you when you've made a mistake, and at least partially guiding you. It's only when you do something unsafe, or uncommon (which includes strange things), that you'll likely need to dig into it.

What's happening is that the main function has to copy because you are passing by value. If it implemented Copy, it would automatically copy it into the function (any changes you make in the function are lost). If you step through the changes below you'll see the issues arise bit by bit.

The solution here is to replace the prototype with a reference (this is a partial update):

fn verify_proofs(commits: &Vec<u8> ...

However, you'll hit the same problem with the proofs vector (as it's the same type), so you update again:

fn verify_proofs(commits: &Vec<u8>, proofs: &Vec<u8>)

From there you'll hit a new issue: you're not passing by reference, you're still trying to pass by copy. That's fine, you need to change your syntax to let it know. Unlike C++, it doesn't automatically detect it and do it--you have to make this choice explicitly.

verify_proofs(&commits, &proofs);

The full playground code of the updated version is here: Rust Playground

The full explanation of what's happening is in the References and Borrowing section of the Rust book.

To help propel your testing forward, since the values need to be mutable in the other function for changes, you would need to make them &mut Vec<u8> in the prototype, and pass them with &mut commits (or proofs). Things get a bit more interesting down the road, so I'd recommend reading through the book chapter on references and borrowing.

4 Likes