use maplit::hashmap; use std::collections::HashMap; pub fn count(nucleotide: char, dna: &str) -> Result { nucleotide_counts(dna)? .remove(&nucleotide) .ok_or(nucleotide) } pub fn nucleotide_counts(dna: &str) -> Result, char> { dna.chars().fold( Ok(hashmap![ 'A' => 0, 'C' => 0, 'G' => 0, 'T' => 0 ]), |acc, n| match n { 'A' | 'C' | 'G' | 'T' => acc.map(|mut hmap| { hmap.entry(n).and_modify(|e| *e += 1).or_insert(0); hmap }), _ => Err(n), }, ) }