Created
September 21, 2019 10:20
-
-
Save rust-play/ab1995dcad67d1e64434ab505550d898 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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