Build HashSet from a vector in Rust
<p>I want to build a <code>HashSet&lt;u8&gt;</code> from a <code>Vec&lt;u8&gt;</code>. I'd like to do this</p>

<ol>
<li>in one line of code,</li>
<li>copying the data only once,</li>
<li>using only <code>2n</code> memory, </li>
</ol>

<p>but the only thing I can get to compile is this piece of .. junk, which I think copies the data twice and uses <code>3n</code> memory.</p>

<pre><code>fn vec_to_set(vec: Vec&lt;u8&gt;) -&gt; HashSet&lt;u8&gt; {
    let mut victim = vec.clone();
    let x: HashSet&lt;u8&gt; = victim.drain(..).collect();
    return x;
}
</code></pre>

<p>I was hoping to write something simple, like this:</p>

<pre><code>fn vec_to_set(vec: Vec&lt;u8&gt;) -&gt; HashSet&lt;u8&gt; {
    return HashSet::from_iter(vec.iter());
}
</code></pre>

<p>but that won't compile:</p>

<pre class="lang-none prettyprint-override"><code>error[E0308]: mismatched types
 --&gt; &lt;anon&gt;:5:12
  |
5 |     return HashSet::from_iter(vec.iter());
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found &amp;u8
  |
  = note: expected type `std::collections::HashSet&lt;u8&gt;`
  = note:    found type `std::collections::HashSet&lt;&amp;u8, _&gt;`
</code></pre>

<p>.. and I don't really understand the error message, probably because I need to RTFM.</p>