title: Rust cannot return value referencing local variable
body: <p>I guess this is a quite basic Rust question, but I have this piece of code</p>
  <pre><code>struct Stock&lt;'a&gt; {
      name: &amp;'a str,
      value: u8,
  }
  impl&lt;'a&gt; Stock&lt;'a&gt; {
      fn read(file: &amp;mut File) -&gt; Stock&lt;'a&gt; {
          let mut value = vec![0; 1];
          file.read(&amp;mut value);
          let name_size = read_u32(file);
          let mut name = vec![0; name_size as usize];
          file.read(&amp;mut name);
          return Stock {
              name: str::from_utf8(&amp;name).unwrap(),
              value: value[0],
          };
      }
  }
  </code></pre>
  <p>and of course, it doesn't work, because I am referencing a local value name. To my limited knowledge, this is because outside the scope of <code>read</code>, <code>name</code> doesn't exist. However wouldn't <code>from_utf8</code> create a new variable that is not scoped to <code>read</code>?</p>
  <p>Also, I've read a bit about other people with this issue, and the suggestion is always to switch to the owned <code>String</code>. But why can't I do something such as <code>name: str::from_utf8(&amp;name).unwrap().to_owned()</code>?</p>