A while back there was talk about trying to make pattern matching (particularly when ref, match, and * are involved) more ergonomic and stumbled across a particularly annoying edge case which reminded me of it.
Is there any more news on this?
My particular example is that I’m iterating over a Vec<(usize, Bot)> and want to update each Bot. Currently I’ve got to write a big mess of &mut and ref to make the compiler happy:
for &mut (tok, ref mut bot) in &mut self.bots {
let actions = bot.tick(&self.game_state);
debug!("Bot {} executed {:?}", tok, actions);
}
Whereas it’d be much easier (and arguably just as understandable) if pattern matching could elide away the boilerplate so I can write something like this:
for (tok, mut bot) in &mut self.bots {
let actions = bot.tick(&self.game_state);
debug!("Bot {} executed {:?}", tok, actions);
}
In theory the compiler should be able to automatically dereference the tuple reference, then that because mut bot is trying to use a mutably borrowed thing it’ll automatically insert ref mut bot.
Hopefully in the future we’ll discuss how to extended that to all patterns, not only match, which would include for loops, fn/closure arguments, let bindings, etc…
But that would be a whole new RFC, so we should probably wait until we get more experience with only match.
Thanks @kennytm for the correction, glad to know the RFC already covers this!