title: Are unwrap()s under patterns optimized away?
body: <p>I have some code where I often use <code>unwrap()</code> under patterns, where I can be sure it won't panic. Some of those pieces are in performance-critical functions, so I was wondering if it would be a good idea to get rid of these <code>unwrap()</code>s in favor of unchecked variants of applicable functions. However, I didn't see any difference with <code>#[bench]</code> tests and the ASM for both variants looks pretty similar to me (though I'm not an expert).</p>
  
  <p>It appears that Rust is able to optimize such cases away; am I right or should I use unchecked functions instead of <code>unwrap()</code>?</p>
  
  <p>MCVE:</p>
  
  <pre><code>use self::Foo::*;
  use self::Error::*;
  
  #[derive(Debug)]
  enum Foo {
      Bar(Box&lt;Foo&gt;),
      Baz
  }
  
  #[derive(Debug)]
  enum Error {
      NotBar
  }
  
  impl Foo {
      fn bar_mut_ref(&amp;mut self) -&gt; Result&lt;&amp;mut Foo, Error&gt; {
          match *self {
              Bar(ref mut foo) =&gt; Ok(foo),
              _ =&gt; Err(NotBar)
          }
      }
  
      fn bar_mut_ref_unchecked(&amp;mut self) -&gt; &amp;mut Foo {
          match *self {
              Bar(ref mut foo) =&gt; foo,
              _ =&gt; panic!("bar_mut_ref_unchecked() called on a non-Bar!")
          }
      }
  
      fn bazify(&amp;mut self) {
          match *self {
              Bar(_) =&gt; { *self = Baz },
              _ =&gt; ()
          }
      }
  }
  
  fn do_stuff_with_foo(foo: &amp;mut Foo) {
      match *foo {
          Bar(_) =&gt; {
              foo.bar_mut_ref().unwrap().bazify(); // is _unchecked() better here?
              // underscore was used because foo is assigned to a new value here
          },
          _ =&gt; {}
      }
  }
  
  fn main() {
      let mut foo = Bar(Box::new(Bar(Box::new(Baz))));
      do_stuff_with_foo(&amp;mut foo);
      println!("{:?}", foo);
  }
  </code></pre>