Skip to content

Commit 1b81921

Browse files
committed
Use internal iteration in k_smallest
1 parent 2c9315c commit 1b81921

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/k_smallest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(mut iter: I, k: usize) -
66

77
let mut heap = iter.by_ref().take(k).collect::<BinaryHeap<_>>();
88

9-
for i in iter {
9+
iter.for_each(|i| {
1010
debug_assert_eq!(heap.len(), k);
1111
// Equivalent to heap.push(min(i, heap.pop())) but more efficient.
1212
// This should be done with a single `.peek_mut().unwrap()` but
1313
// `PeekMut` sifts-down unconditionally on Rust 1.46.0 and prior.
1414
if *heap.peek().unwrap() > i {
1515
*heap.peek_mut().unwrap() = i;
1616
}
17-
}
17+
});
1818

1919
heap
2020
}

0 commit comments

Comments
 (0)