HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustCritical

Is there a more concise or declarative way to initialize a HashMap?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
conciseinitializetherewaydeclarativehashmapmore

Problem

I'm using a HashMap to count the occurrences of different characters in a string:

let text = "GATTACA";
let mut counts: HashMap = HashMap::new();
counts.insert('A', 0);
counts.insert('C', 0);
counts.insert('G', 0);
counts.insert('T', 0);

for c in text.chars() {
    match counts.get_mut(&c) {
        Some(x) => *x += 1,
        None => (),
    }
}


Is there a more concise or declarative way to initialize a HashMap? For example in Python I would do:

counts = { 'A': 0, 'C': 0, 'G': 0, 'T': 0 }


or

counts = { key: 0 for key in 'ACGT' }

Solution

You can use iterators to emulate the dictionary comprehension, e.g.

let counts = "ACGT".chars().map(|c| (c, 0_i32)).collect::>();


or even for c in "ACGT".chars() { counts.insert(c, 0) }.

Also, one can write a macro to allow for concise initialisation of arbitrary values.

macro_rules! hashmap {
    ($( $key: expr => $val: expr ),*) => {{
         let mut map = ::std::collections::HashMap::new();
         $( map.insert($key, $val); )*
         map
    }}
}


used like let counts = hashmap!['A' => 0, 'C' => 0, 'G' => 0, 'T' => 0];.

Code Snippets

let counts = "ACGT".chars().map(|c| (c, 0_i32)).collect::<HashMap<_, _>>();
macro_rules! hashmap {
    ($( $key: expr => $val: expr ),*) => {{
         let mut map = ::std::collections::HashMap::new();
         $( map.insert($key, $val); )*
         map
    }}
}

Context

Stack Overflow Q#28392008, score: 112

Revisions (0)

No revisions yet.