我想用 Vec<u8>
做一个 HashSet<u8>
,我想做这个
2n
内存,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<u8>) -> HashSet<u8> {
let mut victim = vec.clone();
let x: HashSet<u8> = victim.drain(..).collect();
return x;
}
我希望写一些简单的东西,像这样:
fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> {
return HashSet::from_iter(vec.iter());
}
但这不会汇编:
error[E0308]: mismatched types
--> <anon>:5:12
|
5 | return HashSet::from_iter(vec.iter());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found &u8
|
= note: expected type `std::collections::HashSet<u8>`
= note: found type `std::collections::HashSet<&u8, _>`
. . 我真的不明白错误消息,可能是因为我需要 RTFM。