I want to build a HashSet
from a Vec
. I\'d like to do this
The following should work nicely; it fulfills your requirements:
use std::collections::HashSet;
use std::iter::FromIterator;
fn vec_to_set(vec: Vec) -> HashSet {
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 body
I'm not sure which version of Rust you are using, but on current stable (1.12) to_iter()
doesn't exist