I have a struct
that holds mutable references to trait objects:
trait Task {
fn do_it(&mut self);
}
struct Worker<'a> {
tasks: Vec<&'a mut Task>,
}
In a method of Worker
, I want to iterate over the tasks and call their do_it
:
impl<'a> Worker<'a> {
pub fn work(&mut self) {
for task in self.tasks.iter() {
self.work_one(*task);
}
}
fn work_one(&self, task: &mut Task) {
task.do_it();
}
}
Sadly, the borrow checker does not let me do it:
error[E0389]: cannot borrow data mutably in a `&` reference
--> src/main.rs:12:27
|
12 | self.work_one(*task);
| ^^^^^ assignment into an immutable reference
I cannot make Worker
generic because I want it to hold tasks of many types. I also need tasks to be mutable. How do I do it in Rust?