I have a struct App:
struct App { cmd: Command } that owns a command of type Command:
enum Command { Cmd1 { flag: bool } } (I use StructOpt to derive a command line interface from that.)
To execute the right command I have a function like this:
impl App { fn execute(&mut self) { match &self.cmd { Command::Cmd1 { flag } => self.do_cmd1(*flag) }; } } where I handle the actual execution in an extra function do_cmd1(&mut self, flag: bool) to keep execute clean. However, this does not work since in self.do_cmd1(*flag) I borrow self as mutable and also as immutable via *flag which belongs to cmd which in turn belongs to self.
My question is: What would be the proper way to access flag in do_cmd1 that respects the borrowing rules?
Clarification: I need this to also work for things like
enum Command { Cmd2 { text: String } } where the variant's field is not Copy.
*selfas mutable becauseself.history[..]is also borrowed as immutable, How can I call a mutating method while holding a reference to self?, and perhaps Passing mutable self reference to method of owned object (that last one swaps the receiver and argument ofdo_cmd1but the suggestions of the answers apply just as well).