title: How can I simultaneously iterate over a Rust HashMap and modify some of its values?
body: <p>I'm trying Advent of Code in Rust this year, as a way of learning the language. I've parsed the input (from day 7) into the following structure:</p>
  
  <pre><code>struct Process {
      name: String,
      weight: u32,
      children: Vec&lt;String&gt;,
      parent: Option&lt;String&gt;
  }
  </code></pre>
  
  <p>These are stored in a <code>HashMap&lt;String, Process&gt;</code>.  Now I want to iterate over the values in the map and update the parent values, based on what I find in the parent's "children" vector.</p>
  
  <p>What doesn't work is</p>
  
  <pre><code>for p in self.processes.values() {
      for child_name in p.children {
          let mut child = self.processes.get_mut(child_name).expect("Child not found.");
          child.parent = p.name;
      }
  }
  </code></pre>
  
  <p>I can't have both a mutable reference to the <code>HashMap</code> (<code>self.processes</code>) and a non-mutable reference, or two mutable references.</p>
  
  <p>So, what is the most idiomatic way to accomplish this in Rust?  The two options I can see are:</p>
  
  <ol>
  <li>Copy the parent/child relationships into a new temporary data structure in one pass, and then update the Process structs in a second pass, after the immutable reference is out of scope.</li>
  <li>Change my data structure to put "parent" in its own HashMap.</li>
  </ol>
  
  <p>Is there a third option?</p>