I have a rust program that I am creating that repeatedly runs a function in a thread, which causes a value moved error.
Here's some example code (pretty much the same as mine but simplified to get to the point and with a few names changed around)
use std::{thread}
struct Foo {
bar: bool
}
impl Foo {
fn new() -> Self {
Foo { bar: false };
}
fn do_something2(self) {
// do something
// technically could be simplified here to self.bar = !some_condition, but someone will
// probably complain about it, not really relevant to the issue anyways
if some_condition {
self.bar = false;
}
}
}
fn do_something(mut foo: Foo) {
foo.bar = true;
thread::spawn(|| {
while foo.bar {
foo.do_something2();
}
});
}
fn main() {
let mut foo = Foo::new();
do_something(&mut foo);
// other code
}
I am not sure how I would stop the variable from being moved. In this example, it could technically be avoided by implementing the Copy trait, but my struct has a Vec as one of the values, so I cannot use Copy and need to find a different way.