patternrustMajor
Build HashSet from a vector in Rust
Viewed 0 times
hashsetfromvectorrustbuild
Problem
I want to build a
but the only thing I can get to compile is this piece of .. junk, which I think copies the data twice and uses
I was hoping to write something simple, like this:
but that won't compile:
.. and I don't really understand the error message, probably because I need to RTFM.
HashSet from a Vec. I'd like to do this- in one line of code,
- copying the data only once,
- using only
2nmemory,
but the only thing I can get to compile is this piece of .. junk, which I think copies the data twice and uses
3n memory.fn vec_to_set(vec: Vec) -> HashSet {
let mut victim = vec.clone();
let x: HashSet = victim.drain(..).collect();
return x;
}I was hoping to write something simple, like this:
fn vec_to_set(vec: Vec) -> HashSet {
return HashSet::from_iter(vec.iter());
}but that won't compile:
error[E0308]: mismatched types
--> :5:12
|
5 | return HashSet::from_iter(vec.iter());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found &u8
|
= note: expected type std::collections::HashSet
= note: found type std::collections::HashSet
.. and I don't really understand the error message, probably because I need to RTFM.
Solution
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:
Call it like
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
use std::collections::HashSet;
use std::iter::FromIterator;
fn hashset(data: &[u8]) -> HashSet {
HashSet::from_iter(data.iter().cloned())
}Call it like
hashset(&v) where v is a Vec 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.Code Snippets
use std::collections::HashSet;
use std::iter::FromIterator;
fn hashset(data: &[u8]) -> HashSet<u8> {
HashSet::from_iter(data.iter().cloned())
}Context
Stack Overflow Q#39803237, score: 98
Revisions (0)
No revisions yet.