Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Forked from anonymous/playground.rs
Created January 6, 2018 14:23
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 whatalnk/74ec185473a752dae06a1163e90ad7ff to your computer and use it in GitHub Desktop.
Save whatalnk/74ec185473a752dae06a1163e90ad7ff to your computer and use it in GitHub Desktop.
Rust code shared from the playground
use std::collections::HashMap;
fn make_freq_table<'a>(
data: &'a [u16],
counter: HashMap<&'a u16, usize>,
) -> HashMap<&'a u16, usize> {
let mut c = counter;
for x in data {
let entry = c.entry(x).or_insert(0);
*entry += 1;
}
c
}
fn main() {
let mut c = HashMap::new();
let data = &[1, 2, 3, 4, 5];
c = make_freq_table(data, c);
c = make_freq_table(data, c);
c = make_freq_table(data, c);
println!("{:?}", c);
}
@whatalnk
Copy link
Author

whatalnk commented Jan 6, 2018

Update counter.

Take data and HashMap, update HashMap, return HashMap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment