I am making a bot. When the bot receives a message it needs to check all the commands if they trigger on the message and if yes - perform an action.
So I have a vector of commands (Command
trait) in main struct:
struct Bot {
cmds: Vec<Box<Command>>,
}
Everything is good until I try to make a list of triggered commands and to use them later in (&self mut)
method:
let mut triggered: Vec<Box<command::Command>>;
for c in &self.cmds {
if c.check(&message) {
triggered.push(c.clone());
}
}
Error:
bot.rs:167:44: 167:56 error: mismatched types:
expected `Box<Command>`,
found `&Box<Command>`
(expected box,
found &-ptr) [E0308]
What am I doing wrong here? I tried a lot but nothing helps. Initially I was doing the following:
for c in &self.cmds {
if c.check(&message) {
c.fire(&message, self);
}
}
but it gave me:
bot.rs:172:46: 172:50 error: cannot borrow `*self` as mutable because `self.cmds` is also borrowed as immutable [E0502]
bot.rs:172
c.fire(&message, self);
So I stackoverflowed it and came to solution above.