summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch1/ch1-hashmap-hashset.rs
blob: 1fa92298b2a0446435dd169d5f641376097e0dd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::collections::{HashMap, HashSet};

fn main() {

  let input_text = "does this work
  i dont know
  how rust works";

  let mut character_counts = HashMap::new();

  let mut n_lines = 0u32;

  for l in input_text.lines() {
    n_lines = n_lines + 1;

    let mut chars_for_line = HashSet::new();

    for c in l.chars() {
      if chars_for_line.contains(&c) {
        continue
      }
      let c_count = character_counts.entry(c).or_insert(0u32);
      *c_count += 1;
      chars_for_line.insert(c);
    }
  }

  for (c, c_count) in &character_counts {
    if *c_count == n_lines {
      println!("{}", c);
    }
  }
}