Skip to content

Commit

Permalink
Fix warning about borrowing maps
Browse files Browse the repository at this point in the history
Full context in this issue - rust-lang/rust#59159

It is currently a warning to borrow a map as mutable and then modify it.

Replacing such uses with the entry API.
  • Loading branch information
nindalf committed Jun 16, 2019
1 parent cc9005b commit d238a1a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
7 changes: 3 additions & 4 deletions src/day02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ fn character_counts(s: &str, n: usize) -> bool {
fn counts(s: &str) -> HashMap<char, usize> {
let mut result: HashMap<char, usize> = HashMap::new();
for c in s.chars() {
match result.get(&c) {
Some(cur) => result.insert(c, cur + 1),
None => result.insert(c, 1),
};
result.entry(c)
.and_modify(|cur| *cur = *cur + 1)
.or_insert(1);
}
result
}
Expand Down
11 changes: 7 additions & 4 deletions src/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ fn count_dupes(big_cloth: &HashMap<(u32, u32), i32>) -> u32 {
fn find_perfect(clothes: &[Cloth], big_cloth: &HashMap<(u32, u32), i32>) -> i32 {
let mut successes = HashMap::new();
big_cloth.values().filter(|x| **x > 0).for_each(|x| {
match successes.get(&x) {
Some(cur) => successes.insert(x, cur + 1),
None => successes.insert(x, 1),
};
successes.entry(x)
.and_modify(|cur| *cur = *cur + 1)
.or_insert(1);
// match successes.get(&x) {
// Some(cur) => successes.insert(x, cur + 1),
// None => successes.insert(x, 1),
// };
});
for cloth in clothes {
match successes.get(&cloth.id) {
Expand Down
15 changes: 5 additions & 10 deletions src/day04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ fn minute_most_often_asleep(naps: &[Nap]) -> (i32, i32) {
}
for nap in naps {
for minute in nap.start.time().minute()..nap.end.time().minute() {
let cur = &minutes[&minute];
minutes.insert(minute, cur + 1);
minutes.entry(minute)
.and_modify(|cur| *cur = *cur + 1);
}
}
let mut high_naps = 0;
Expand Down Expand Up @@ -130,14 +130,9 @@ fn process_logs(s: &str) -> HashMap<i32, Vec<Nap>> {
end: time,
};
let guard = current_guard.unwrap();
match guard_naps.get_mut(&guard) {
Some(v) => {
v.push(nap);
}
None => {
guard_naps.insert(guard, vec![nap]);
}
}
guard_naps.entry(guard)
.or_default()
.push(nap);
start_time = None;
}
Err(()) => panic!("error while parsing"),
Expand Down
7 changes: 2 additions & 5 deletions src/day06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@ impl Grid {
let nearest = tile.unwrap();
if self.is_edge(i, j) {
result.remove(&nearest);
}
let r = result.get(&nearest);
if r.is_none() {
continue;
}
let times = r.unwrap();
result.insert(nearest, times + 1);
result.entry(nearest)
.and_modify(|times| *times = *times + 1);
}
}
*result.values().max().unwrap()
Expand Down
11 changes: 7 additions & 4 deletions src/day09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ impl Board {
if marble % 23 == 0 {
current_index = (self.marbles.len() + current_index - 7) % self.marbles.len();
let score = marble + self.marbles.remove(current_index);
match self.scores.get(&current_player) {
Some(n) => self.scores.insert(current_player, score + n),
None => self.scores.insert(current_player, score),
};
self.scores.entry(current_player)
.and_modify(|n| *n = *n + score)
.or_insert(score);
// match self.scores.get(&current_player) {
// Some(n) => self.scores.insert(current_player, score + n),
// None => self.scores.insert(current_player, score),
// };
continue;
}

Expand Down

0 comments on commit d238a1a

Please # to comment.