Skip to content

Instantly share code, notes, and snippets.

Created March 21, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/b89d43f82ec2f7572343 to your computer and use it in GitHub Desktop.
Save anonymous/b89d43f82ec2f7572343 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::collections::HashMap;
fn nucleotide_counts(nucleotides: &str) -> HashMap<char, usize> {
let mut nucleotide = HashMap::new();
for n in nucleotides.chars() {
let counter = nucleotide.entry(n) // gets the given key's corrosponding entry in the map for in-place manipulation
.or_insert(0); // ..or insert 0 if its not present already
*counter += 1; // Now increment the entry, so it's 1 for all new keys or plus one for all other.
}
nucleotide
}
fn count(nucleotide: char) -> usize {
let map = nucleotide_counts("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC");
match map.get(&nucleotide) {
Some(x) => *x,
_ => 0,
}
}
fn main() {
println!("{:?}", count(''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment