Skip to content

Commit 431a537

Browse files
authored
fix(heap.rs): get from empty heap returns None (rust-lang#215)
1 parent 45bf766 commit 431a537

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/data_structures/heap.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ where
105105
type Item = T;
106106

107107
fn next(&mut self) -> Option<T> {
108-
let next = if self.count == 0 {
109-
None
110-
} else {
111-
// This feels like a function built for heap impl :)
112-
// Removes an item at an index and fills in with the last item
113-
// of the Vec
114-
let next = self.items.swap_remove(1);
115-
Some(next)
116-
};
108+
if self.count == 0 {
109+
return None;
110+
}
111+
// This feels like a function built for heap impl :)
112+
// Removes an item at an index and fills in with the last item
113+
// of the Vec
114+
let next = Some(self.items.swap_remove(1));
117115
self.count -= 1;
118116

119117
if self.count > 0 {
@@ -159,6 +157,11 @@ impl MaxHeap {
159157
#[cfg(test)]
160158
mod tests {
161159
use super::*;
160+
#[test]
161+
fn test_empty_heap() {
162+
let mut heap = MaxHeap::new::<i32>();
163+
assert_eq!(heap.next(), None);
164+
}
162165

163166
#[test]
164167
fn test_min_heap() {

0 commit comments

Comments
 (0)