In my code, I have a struct which stores messages for users in a HashMap called messages
. Inside a function which takes &mut self
as an arguement, when I find messages for the user, I would like to modify the value of this field to remove these messages, so that the user doesn't get the same messages twice. However, I get the error that I can't borrow self as mutable because I've borrowed it as immutable at the start of the pattern match.
match self.messages.find(&username) {
Some(message_array) => {
//do some stuff to send the messages
self.messages.remove(&username);
},
I found this question modifying a field while pattern matching on it, but the accepted answer for it doesn't seem to address my question -- I don't understand how the line &Tokenizer { state: InATag(*) } => { self.state = Outside }
makes self mutable again.