Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 21, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/ab1995dcad67d1e64434ab505550d898 to your computer and use it in GitHub Desktop.
Save rust-play/ab1995dcad67d1e64434ab505550d898 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::cmp::Eq;
use std::collections::HashMap;
use std::hash::Hash;
pub fn occurrence_map<I, E>(target: I) -> HashMap<E, usize>
where
I: IntoIterator<Item = E>,
E: Eq + Hash,
{
let mut hash_map = HashMap::new();
for el in target {
*hash_map.entry(el).or_insert(0) += 1;
}
hash_map
}
fn main() {
occurrence_map(&["a", "b", "a"]);
occurrence_map(vec![String::from("a"), String::from("b"), String::from("a")]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment