From 02481656966b0a8dfc95cf3c22bcc049660ff7d4 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sat, 26 Dec 2020 17:48:38 +0000 Subject: Move Rust exercises in a subdirectory --- rust/nucleotide-count/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rust/nucleotide-count/src/lib.rs (limited to 'rust/nucleotide-count/src') 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 @@ +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), + }, + ) +} -- cgit v1.2.3