Skip to content

Commit

Permalink
Create a bunch of methods for the Author struct
Browse files Browse the repository at this point in the history
- You can initialize a new author with Author::new which will create a new
  author who has been a very good boy indeed.
- Update how many times he is naughty
- Filter out all the words he didn't say
- And check if he actually is naughty
  • Loading branch information
sondr3 committed Oct 15, 2018
1 parent 4fcd0ca commit b4b14ec
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ impl PartialEq for Author {
}

impl Author {
fn new(name: &str, curses: HashMap<&'static str, usize>) -> Author {
fn new(name: &str) -> Self {
let name = name.to_string();
let curses: Vec<&str> = CURSES.lines().collect();
let mut map: HashMap<String, usize> = HashMap::new();
for curse in curses {
map.insert(curse.to_string(), 0);
}
Author {
name, curses
name,
curses: map,
}
}

fn update_occurrence(&mut self, curse: &str) {
self.curses.entry(curse.to_string()).and_modify(|i| *i += 1);
}

fn filter_occurrences(&mut self) {
self.curses.retain(|_, val| val > &mut 0);
}

fn is_not_naughty(&self) -> bool {
self.curses.is_empty()
}
}

fn main() -> Result<(), Box<Error>> {
Expand Down

0 comments on commit b4b14ec

Please sign in to comment.