I'm using a HashMap
to count the occurrences of different characters in a string:
let text = "GATTACA";
let mut counts: HashMap<char, i32> = 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' }