title: What is the difference between the ? operator and returning Err(e)?
body: <p><a href="https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.html" rel="nofollow noreferrer">The ? operator for easier error handling</a> from the Rust Edition Guide says that this line:</p>
  <pre class="lang-rust prettyprint-override"><code>let mut f = File::open(&quot;username.txt&quot;)?;
  </code></pre>
  <p>is a shorthand for this entire match statement:</p>
  <pre class="lang-rust prettyprint-override"><code>let f = File::open(&quot;username.txt&quot;);
  
  let mut f = match f {
      Ok(file) =&gt; file,
      Err(e) =&gt; return Err(e),
  };
  </code></pre>
  <p>What is the actual difference between the <code>?</code> operator and returning <code>Err(e)</code>?</p>
  <p>Consider the code:</p>
  <pre class="lang-rust prettyprint-override"><code>fn main() {
      println!(&quot;{:?}&quot;, foo());
  }
  
  fn foo() -&gt; Result&lt;(), String&gt; {
      Err(&quot;error&quot;)?;          // (1) this is fine
      //return Err(&quot;error&quot;);  // (2) compilation error: 'mismatched types'
      
      Ok(())
  }
  </code></pre>
  <p>If the <code>?</code> operator was just a sugar-syntax for returning appropriate <code>Err</code> from function then I would expect (1) and (2) to treated in the same way by the compiler. What is the difference we can observe in the example above?</p>