title: How can I introduce a copied variable as mutable in a if-let statement?
body: <p>I have a <code>HashMap&lt;i8, i8&gt;</code> which could contain cycles:</p>
  
  <pre><code>let mut x: HashMap&lt;i8, i8&gt; = HashMap::new();
  x.insert(1, 6);
  x.insert(3, 5);
  x.insert(5, 1);
  </code></pre>
  
  <p>To get the final value for <code>3</code>, it should first lookup <code>x[3]</code>, then <code>x[5]</code> and finally <code>x[1]</code> which should yield <code>6</code>. I decided to use a <code>while let</code> loop:</p>
  
  <pre><code>let mut y = x[&amp;3]; // y: i8
  while let Some(&amp;z) = x.get(&amp;y) {
      y = z;
  }
  
  println!("{}", y);
  
  x.insert(0, 0);
  </code></pre>
  
  <p>This works fine, but it would <code>panic!</code> if <code>3</code> is not in the map. As I don't want to do anything about the <code>None</code> case, I want to use a <code>if let</code> (similar to the <code>while let</code> used).</p>
  
  <p>I have tried some notations:</p>
  
  <ol>
  <li><code>if let Some(&amp;y) = x.get(&amp;3)</code>: copies the value, but y is immutable (<code>y: i8</code>)</li>
  <li><code>if let Some(mut y) = x.get(&amp;3)</code>: y is mutable, but the value is borrowed (<code>mut y: &amp;i8</code>)</li>
  <li><code>if let mut Some(&amp;y) = x.get(&amp;3)</code>: my target: mutable copy, but invalid syntax (<code>mut y: i8</code>)</li>
  </ol>
  
  <p>(All variants are available at <a href="https://play.rust-lang.org/?gist=8b4c4dbc59ac6293f1c65b260338c63f&amp;version=stable&amp;backtrace=0" rel="nofollow noreferrer">Rust Playground</a>, but you need to comment out the third try, as it is invalid syntax)</p>
  
  <p>I would not argue about the second variant, but I need to insert values into my map in the body of the <code>if let</code>. As the map remains borrowed, I can't insert anymore. All I would need is that the value in <code>Some(y)</code> is copied, and <code>y</code> is mutable, so that the borrow checker is satisfied and I can do my recursive lookups.</p>