title: Effectively take ownership of a borrowed reference to return from a function
body: <p>I'm trying to write a simple <code>prompt</code> function that returns an input string without trailing newlines, but I'm not able to return my result because <code>input</code> does not live long enough.  I understand that <code>String::trim_right_matches</code> is returning a borrowed reference to a portion of <code>input: String</code>, but I can't figure out how to take ownership of this data or copy it somehow to return it.</p>
  
  <p>I've been spinning my wheels for a few hours now with no luck, though I have learned this 'fighting with the borrow checker' is a rite of passage for newcomers to Rust, so I guess I'm not alone.</p>
  
  <pre><code>use std::io;
  use std::io::Write;
  
  fn main() {
      println!("you entered: {}", prompt("enter some text: "));
  }
  
  fn prompt(msg: &amp;str) -&gt; &amp;str {
      print!("{}", msg);
  
      io::stdout().flush()
          .ok()
          .expect("could not flush stdout");
  
      let mut input = String::new();
  
      io::stdin()
          .read_line(&amp;mut input)
          .expect("failed to read from stdin");
  
      input.trim_right_matches(|c| c == '\r' || c == '\n')
  }
  </code></pre>
  
  <p>Intuition tells me I need to have <code>fn prompt(prompt: &amp;str) -&gt; str</code> instead of <code>-&gt; &amp;str</code>, but I haven't been able to effect that in a way the compiler accepts.</p>
  
  <pre><code>error: `input` does not live long enough
    --&gt; src/main.rs:22:5
     |
  22 |     input.trim_right_matches(|c| c == '\r' || c == '\n').clone()
     |     ^^^^^ does not live long enough
  23 | }
     | - borrowed value only lives until here
     |
  note: borrowed value must be valid for the anonymous lifetime #1 defined on the block at 9:32...
    --&gt; src/main.rs:9:33
     |
  9  | fn prompt(msg: &amp;str) -&gt; &amp;str {
     |                                 ^
  
  error: aborting due to previous error
  </code></pre>