title: Why does my variable not live long enough?
body: <p>I have a simple piece of code that is supposed to read a file into a vector by lines</p>
  
  <pre><code>use std::io::{self, Read};
  use std::fs::File;
  
  fn file_to_vec(filename: &amp;str) -&gt; Result&lt;Vec&lt;&amp;str&gt;, io::Error&gt; {
      let mut file = try!(File::open(filename));
      let mut string = String::new();
      try!(file.read_to_string(&amp;mut string));
      string.replace("\r", "");
  
      let data: Vec&lt;&amp;str&gt; = string.split('\n').collect();
  
      Ok(data)
  }
  
  fn main() {}
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `string` does not live long enough
    --&gt; src/main.rs:10:27
     |
  10 |     let data: Vec&lt;&amp;str&gt; = string.split('\n').collect();
     |                           ^^^^^^ does not live long enough
  ...
  13 | }
     | - borrowed value only lives until here
     |
  note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 4:1...
    --&gt; src/main.rs:4:1
     |
  4  | / fn file_to_vec(filename: &amp;str) -&gt; Result&lt;Vec&lt;&amp;str&gt;, io::Error&gt; {
  5  | |     let mut file = try!(File::open(filename));
  6  | |     let mut string = String::new();
  7  | |     try!(file.read_to_string(&amp;mut string));
  ...  |
  12 | |     Ok(data)
  13 | | }
     | |_^
  </code></pre>
  
  <p>Why do I keep getting this error? How do I fix this? I imagine that it has something to do with the <code>split</code> method. </p>
  
  <p>I could return the string and then split it into a <code>Vec</code> in the main function, but I really want to return a vector.</p>