title: When is tail recursion guaranteed in Rust?
body: <h1>C language</h1>
  
  <p>In the C programming language, it's easy to have <strong>tail recursion</strong>:</p>
  
  <pre class="lang-c prettyprint-override"><code>int foo(...) {
      return foo(...);
  }
  </code></pre>
  
  <p>Just return as is the return value of the recursive call. It is especially important when this recursion may repeat a thousand or even a million times. It would use a lot of <strong>memory on the stack</strong>.</p>
  
  <h1>Rust</h1>
  
  <p>Now, I have a Rust function that might recursively call itself a million times:</p>
  
  <pre><code>fn read_all(input: &amp;mut dyn std::io::Read) -&gt; std::io::Result&lt;()&gt; {
      match input.read(&amp;mut [0u8]) {
          Ok (  0) =&gt; Ok(()),
          Ok (  _) =&gt; read_all(input),
          Err(err) =&gt; Err(err),
      }
  }
  </code></pre>
  
  <p>(this is a minimal example, the real one is more complex, but it captures the main idea)</p>
  
  <p>Here, the return value of the recursive call is returned as is, but:</p>
  
  <p><strong>Does it guarantee that the Rust compiler will apply a tail recursion?</strong></p>
  
  <p>For instance, if we declare some variable that needs to be destroyed like a <code>std::Vec</code>, will it be destroyed just before the recursive call (which allows for tail recursion) or after the recursive call returns (which forbids the tail recursion)?</p>