title: Is there a way to trim a String without allocating another one?
body: <p>I have a file in the CSV format with a first column of data that represents item code optionally ended with <code>&quot;UNIUNI&quot;</code> or mixed case of these chars, loaded by means of a barcode reader. I need to trim away the last <code>&quot;UNI&quot;</code>s.</p>
  <p>I wrote this function:</p>
  <pre><code>fn main() {
      // Ok: from &quot;9846UNIUNI&quot; to &quot;9846&quot;
      println!(&quot;{}&quot;, read_csv_rilev(&quot;9846UNIUNI&quot;.to_string()));
      
      // Wrong: from &quot;9846uniuni&quot; to &quot;9846&quot;
      println!(&quot;{}&quot;, read_csv_rilev(&quot;9846uniuni&quot;.to_string()));
  }
  
  fn read_csv_rilev(code: String) -&gt; String {
      code
          //.to_uppercase() /*Unstable feature in Rust 1.1*/
          .trim_right_matches(&quot;UNI&quot;)
          .to_string()
  }
  </code></pre>
  <p>The ideal function signature looks like:</p>
  <pre><code>fn read_csv_rilev(mut s: &amp;String)
  </code></pre>
  <p>but probably an in-place action on a <code>String</code> is not a good idea. In fact, in the Rust standard library there isn't anything to do this excluding <code>String::pop()</code>.</p>