title: How do I return a reversed string from a function?
body: <p>I want to write a function that would reverse a string and return a reference to the returned string. I thought I would be able to specify that the returned reference has the same lifetime as the provided reference.</p>
  
  <pre><code>fn reverse_string&lt;'a&gt;(input: &amp;'a str) -&gt; &amp;'a str {
      let res: &amp;'a str = input.chars().rev().collect().as_slice();
      res
  }
  </code></pre>
  
  <p>This was my attempt, and I receive the error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0282]: type annotations needed
   --&gt; src/lib.rs:2:24
    |
  2 |     let res: &amp;'a str = input.chars().rev().collect().as_slice();
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `B`
    |
    = note: type must be known at this point
  </code></pre>
  
  <p>I thought the type specification would resolve this. Lifetimes are a bit confusing to me, so I was hoping someone may be able to explain how to do this properly, or if I'm completely off base to begin with.</p>