Pattern matching problem
⚓ Rust 📅 2026-02-21 👤 surdeus 👁️ 1Hello. I'm trying to pattern-match an enum alongside a passed value in a loop, and have a mutable reference to a field in the enum. However, I can't seem to figure out the correct way to write this.
This is where I'm currently at:
use std::collections::HashMap;
enum Foo {
One(HashMap<u8, u8>),
Two,
}
impl Foo {
fn bar(&mut self, a: u8) {
for i in 0..2 {
match (self, a) {
(Foo::One(h), _) => {
h.get_mut(&0);
}
(Foo::Two, _) => {}
}
}
}
}
fn main() {}
This code results in the compiler complaining about self being moved in the previous iteration of the loop.
Trying to do &self in the match statement makes h.get_mut() fail
Trying to do &mut self in the match statement fails because "the binding is already a mutable borrow"
What do?
4 posts - 2 participants
🏷️ Rust_feed