Build HashSet From A Vector In Rust
Answer :
Because the operation does not need to consume the vector¹, I think it should not consume it. That only leads to extra copying somewhere else in the program:
use std::collections::HashSet; use std::iter::FromIterator; fn hashset(data: &[u8]) -> HashSet<u8> { HashSet::from_iter(data.iter().cloned()) }
Call it like hashset(&v)
where v
is a Vec<u8>
or other thing that coerces to a slice.
There are of course more ways to write this, to be generic and all that, but this answer sticks to just introducing the thing I wanted to focus on.
¹This is based on that the element type u8
is Copy
, i.e. it does not have ownership semantics.
The following should work nicely; it fulfills your requirements:
use std::collections::HashSet; use std::iter::FromIterator; fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> { HashSet::from_iter(vec) }
from_iter()
works on types implementing IntoIterator
, so a Vec
argument is sufficient.
Additional remarks:
you don't need to explicitly
return
function results; you only need to omit the semi-colon in the last expression in its bodyI'm not sure which version of Rust you are using, but on current stable (1.12)
to_iter()
doesn't exist
Moving data ownership
let vec: Vec<usize> = vec![1, 2, 3, 4]; let hash_set: HashSet<usize> = vec.into_iter().collect();
Cloning data
let vec: Vec<usize> = vec![1, 2, 3, 4]; let hash_set: HashSet<usize> = vec.iter().cloned().collect();
Comments
Post a Comment