Skip to content

Implement BinaryHeap::pop_if()#151829

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
max-heller:binary-heap-pop-if
Jan 31, 2026
Merged

Implement BinaryHeap::pop_if()#151829
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
max-heller:binary-heap-pop-if

Conversation

@max-heller
Copy link
Contributor

Implementation of #151828

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 29, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 29, 2026

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@max-heller
Copy link
Contributor Author

I'm getting errors compiling an alloctest test for pop_if():

  error[E0658]: use of unstable library feature `binary_heap_pop_if`
  Error:    --> alloctests/tests/collections/binary_heap.rs:145:35
      |
  145 |     while let Some(popped) = heap.pop_if(|x| *x > 2) {
      |                                   ^^^^^^
      |
      = note: see issue #151828 <https://github.com/rust-lang/rust/issues/151828> for more information
      = help: add `#![feature(binary_heap_pop_if)]` to the crate attributes to enable

I added #![feature(binary_heap_pop_if)] to alloctest/lib.rs alongside a large number of other #![feature] attributes, not sure what I'm missing.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it took me a while to figure out the CI failure...

View changes since this review

/// The worst case cost of `pop_if` on a heap containing *n* elements is *O*(log(*n*)).
#[unstable(feature = "binary_heap_pop_if", issue = "151828")]
pub fn pop_if(&mut self, predicate: impl FnOnce(&T) -> bool) -> Option<T> {
let first = self.data.first()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peek is just a bit nicer.

Suggested change
let first = self.data.first()?;
let first = self.peek()?;

#![feature(allocator_api)]
#![feature(array_into_iter_constructors)]
#![feature(assert_matches)]
#![feature(binary_heap_pop_if)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong lib.rs, you'll need to add the feature to alloctests/src/lib.rs instead. We use this lib.rs only for running the doctests and internal tests of alloc (I can't remember why alloc can't be tested directly just now, but there was some reason).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That did it, thanks!

///
/// # Examples
///
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to add the feature here too, otherwise the doctest will fail 😉

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 30, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 30, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@max-heller
Copy link
Contributor Author

Thanks for the review!
@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 30, 2026
@jhpratt
Copy link
Member

jhpratt commented Jan 31, 2026

@bors r=jhpratt,joboet rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 31, 2026

📌 Commit bae7a19 has been approved by jhpratt,joboet

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 31, 2026
rust-bors bot pushed a commit that referenced this pull request Jan 31, 2026
Rollup of 5 pull requests

Successful merges:

 - #143650 (core: add Option::get_or_try_insert_with)
 - #151726 (Remove duplicated code in `slice/index.rs`)
 - #151812 (Add `shift_{left,right}` on slices)
 - #151829 (Implement `BinaryHeap::pop_if()`)
 - #151838 (Fix typo for Maybe dangling docs)
@rust-bors rust-bors bot merged commit f4c2816 into rust-lang:main Jan 31, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Jan 31, 2026
rust-timer added a commit that referenced this pull request Jan 31, 2026
Rollup merge of #151829 - max-heller:binary-heap-pop-if, r=jhpratt,joboet

Implement `BinaryHeap::pop_if()`

Implementation of #151828
@max-heller max-heller deleted the binary-heap-pop-if branch January 31, 2026 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants