I'm trying to give ownership of a variable to a function in a loop and I have my own boolean to ensure it happens only one time, however the compiler tells me the value was moved in the previous iteration.
Here is an example:
fn take_ownership(a: String) {
println!("{}", a);
}
fn main() {
let mut a = true;
let hello = "Hello".to_string();
for _ in 0..5 {
if a {
a = false;
take_ownership(hello);
}
}
}
With this code, the compiler tells me:
error[E0382]: use of moved value: `hello`
--> src/main.rs:12:28
|
12 | take_ownership(hello);
| ^^^^^ value moved here in previous iteration of loop
Is there a way to tell the compiler "it's ok, I will handle it"? I don't want to use references (&
).