aboutsummaryrefslogtreecommitdiff
path: root/rust/nucleotide-count/src/lib.rs
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 17:48:38 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch)
tree8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/nucleotide-count/src/lib.rs
parent4e2052c4d792540c2f742b2c2a081b11117ed41d (diff)
downloadexercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz
exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/nucleotide-count/src/lib.rs')
-rw-r--r--rust/nucleotide-count/src/lib.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/rust/nucleotide-count/src/lib.rs b/rust/nucleotide-count/src/lib.rs
new file mode 100644
index 0000000..98b0c38
--- /dev/null
+++ b/rust/nucleotide-count/src/lib.rs
@@ -0,0 +1,21 @@
1use maplit::hashmap;
2use std::collections::HashMap;
3
4pub fn count(nucleotide: char, dna: &str) -> Result<usize, char> {
5 nucleotide_counts(dna)?
6 .remove(&nucleotide)
7 .ok_or(nucleotide)
8}
9
10pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, char> {
11 dna.chars().fold(
12 Ok(hashmap![ 'A' => 0, 'C' => 0, 'G' => 0, 'T' => 0 ]),
13 |acc, n| match n {
14 'A' | 'C' | 'G' | 'T' => acc.map(|mut hmap| {
15 hmap.entry(n).and_modify(|e| *e += 1).or_insert(0);
16 hmap
17 }),
18 _ => Err(n),
19 },
20 )
21}