Build HashSet from a vector in Rust

后端 未结 3 1160
谎友^
谎友^ 2021-02-06 21:03

I want to build a HashSet from a Vec. I\'d like to do this

  1. in one line of code,
  2. copying the data only once,<
相关标签:
3条回答
  • 2021-02-06 21:49

    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();
    
    0 讨论(0)
  • 2021-02-06 21:51

    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.

    0 讨论(0)
  • 2021-02-06 21:58

    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 body

    • I'm not sure which version of Rust you are using, but on current stable (1.12) to_iter() doesn't exist

    0 讨论(0)
提交回复
热议问题