title: Return original or modified str
body: <p>I have a function to clean a string that looks something like this:</p>
  
  <pre><code>fn clean(s: &amp;str) -&gt; &amp;str { // but not sure about return type
      if /* needs cleaning */ {
          let cleaned: String = s.chars().filter( /* etc */ ).collect();
          cleaned
      } else {
          s
      }
  }
  </code></pre>
  
  <p>Except this doesn't work as written, because cleaned is <code>String</code>, not a <code>&amp;str</code>.</p>
  
  <p>The goal here is to only perform an allocation if necessary - if the string needs to be modified, I want to replace it with a new one, and if it doesn't, I don't want to call <code>to_string()</code> on it. Ideally, I'd like that to be transparent to the caller, but it doesn't have to be - I have control of the calling code too. Even so, I haven't found a workaround, because if the newly-created <code>String</code>, or even a borrow of it, ends up in some kind of if or else block in the caller, it's lifetime isn't long enough to use in the contexts where the original string is used otherwise. For example, this also doesn't work:</p>
  
  <pre><code>fn caller(s: &amp;str) {
      if needs_cleaning(s) {
          let cleaned = clean(s); // where clean always returns a new String
          s = &amp;cleaned;
      }
  
      / * do stuff with the clean string */
  }
  </code></pre>
  
  <p>What's the right approach here?</p>