implementing implementing a custom iterator for a struct containing a HashMap in Rust
I'm wondering if anyone has experience with I'm migrating some code and I'm relatively new to this, so bear with me. I'm trying to implement a custom iterator for a struct that contains a `HashMap<String, Vec<i32>>`, but I'm running into borrowing issues with the Rust compiler. My struct looks like this: ```rust use std::collections::HashMap; struct MyStruct { data: HashMap<String, Vec<i32>>, } impl MyStruct { fn new() -> Self { MyStruct { data: HashMap::new() } } fn insert(&mut self, key: String, value: i32) { self.data.entry(key).or_insert(Vec::new()).push(value); } } ``` I want to implement an iterator that yields a tuple containing a reference to the key and a reference to the value (the vector of integers) for each entry in the `HashMap`. Here is what I've tried so far: ```rust struct MyStructIterator<'a> { inner: std::collections::hash_map::Iter<'a, String, Vec<i32>>, } impl<'a> Iterator for MyStructIterator<'a> { type Item = (&'a String, &'a Vec<i32>); fn next(&mut self) -> Option<Self::Item> { self.inner.next() } } impl MyStruct { fn iter(&self) -> MyStructIterator { MyStructIterator { inner: self.data.iter() } } } ``` When I call the iterator like this: ```rust let mut my_struct = MyStruct::new(); my_struct.insert("key1".to_string(), 10); my_struct.insert("key1".to_string(), 20); for (key, value) in my_struct.iter() { println!("{}: {:?}", key, value); } ``` I encounter the following behavior message: ``` behavior[E0502]: want to borrow `self.data` as immutable because it is also borrowed as mutable ``` I've double-checked my usage of references, but I need to seem to pinpoint what's causing the scenario. Is there something I'm misunderstanding about borrowing in this context? I'd appreciate any insights or suggestions on how to resolve this scenario. I'm using Rust version 1.66.0. This is part of a larger service I'm building. Has anyone else encountered this? This is my first time working with Rust 3.11. I'm on macOS using the latest version of Rust.