title: What's the difference between &String and &str in a function signature?
body: <p>Considering the following program:</p>
  <pre><code>fn main() {
      let s = String::from(&quot;helloworld&quot;);
      // This works fine
      println!(&quot;The first word is: {}&quot;, first_word(&amp;s));
      // This won't compile
      println!(&quot;The first word is: {}&quot;, first_word(&amp;s[..]));
      // This works fine
      println!(&quot;The first word is: {}&quot;, first_word1(&amp;s));
      // This works fine
      println!(&quot;The first word is: {}&quot;, first_word1(&amp;s[..]));
  }
  
  fn first_word(s: &amp;String) -&gt; &amp;str {
      let bytes = s.as_bytes();
  
      for (i, &amp;item) in bytes.iter().enumerate() {
          if item == b' ' {
              return &amp;s[0..i];
          }
      }
  
      s
  }
  
  fn first_word1(s: &amp;str) -&gt; &amp;str {
      let bytes = s.as_bytes();
  
      for (i, &amp;item) in bytes.iter().enumerate() {
          if item == b' ' {
              return &amp;s[0..i];
          }
      }
  
      &amp;s[..]
  }
  </code></pre>
  <p>It seems that for a &amp;str parameter, you can use either a &amp;String or a &amp;str as argument, but for a &amp;String parameter, only &amp;String is allowed.
  It also seems that if a function returns &amp;str in its signature, it can return either a &amp;String or a &amp;str, the caller of the function won't see any difference.</p>
  <p>So my question is, what is the difference between &amp;String and &amp;str, in parameter types and return types of rust function signatures?</p>