How do I convert between String, &str, Vec<u8> and &[u8]?
<p>A new Rustacean like me struggles with juggling these types: <code>String</code>, <code>&amp;str</code>, <code>Vec&lt;u8&gt;</code>, <code>&amp;[u8]</code>.</p>
<p>In time, I hope to have an epiphany and suddenly get why some library calls use one or the other. Until then, I need help to map out each idiomatic transition.</p>
<p>Given these types:</p>
<pre><code>let st: &amp;str = ...;
let s:  String = ...;
let u:  &amp;[u8] = ...;
let v:  Vec&lt;u8&gt; = ...;
</code></pre>
<p>I think I have figured these out, but are they idiomatic?</p>
<pre class="lang-none prettyprint-override"><code>&amp;str    -&gt; String    String::from(st)
&amp;str    -&gt; &amp;[u8]     st.as_bytes()
String  -&gt; &amp;str      s.as_str()
&amp;[u8]   -&gt; &amp;str      str::from_utf8(u)
Vec&lt;u8&gt; -&gt; String    String::from_utf8(v)
</code></pre>
<p>Ultimately I want a complete table of transitions for these types:</p>
<pre class="lang-none prettyprint-override"><code>&amp;str    -&gt; String
&amp;str    -&gt; &amp;[u8]
&amp;str    -&gt; Vec&lt;u8&gt;
String  -&gt; &amp;str
String  -&gt; &amp;[u8]
String  -&gt; Vec&lt;u8&gt;
&amp;[u8]   -&gt; &amp;str
&amp;[u8]   -&gt; String
&amp;[u8]   -&gt; Vec&lt;u8&gt;
Vec&lt;u8&gt; -&gt; &amp;str
Vec&lt;u8&gt; -&gt; String
Vec&lt;u8&gt; -&gt; &amp;[u8]
</code></pre>