Skip to content

Tracking Issue for Vec::pop_if #122741

Description

@igneousflock

Feature gate: #![feature(vec_pop_if)]

This feature adds the Vec::pop_if method, which takes a predicate, evaluates it with the last element in the Vec if present, and returns the item if the predicate returns true. This makes it possible to conditionally remove the last element without the use of unwrap.

Public API

impl<T> Vec<T> {
    pub fn pop_if(&mut self, f: impl FnOnce(&mut T) -> bool) -> Option<T>;
}

Steps / History

Unresolved Questions

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-api[DEPRECATED; DO NOT USE]
on Mar 19, 2024
added a commit that references this issue on Mar 27, 2024
added a commit that references this issue on Mar 27, 2024

GrigorenkoPV commented on Jan 14, 2025

@GrigorenkoPV
Contributor

Found a piece of code in the wild that could benefit (get rid of an .unwrap()) with this API stabilized:
https://github.com/XAMPPRocky/tokei/blob/edbd5d5cbb0b7ea0081df0265a8ae6e7742a5051/src/language/syntax.rs#L663-L668

Also I myself have found this feature to be useful on couple occasions in personal projects.

This does not look too controversial, so maybe let's stabilize it?

The corresponding VecDeque methods can be added and stabilized later without blocking this.

I don't think I have right to start an FCP, so I will just open a PR instead.

tgross35 commented on Jan 16, 2025

@tgross35
Member

Docs should probably mention that the function does not get run if the vector is empty.

I don't think I have right to start an FCP, so I will just open a PR instead.

You can add the I-libs-api-nominated label with rustbot

GrigorenkoPV commented on Jan 17, 2025

@GrigorenkoPV
Contributor

You can add the I-libs-api-nominated label with rustbot

Thanks, that makes sense

@rustbot label +I-libs-api-nominated

Docs should probably mention that the function does not get run if the vector is empty.

True, it might have side-effects, so it's probably worth mentioning. I will adjust my PR.

Amanieu commented on Jan 21, 2025

@Amanieu
Member

@rfcbot merge

rfcbot commented on Jan 21, 2025

@rfcbot

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

added
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.
on Jan 21, 2025

GrigorenkoPV commented on Jan 22, 2025

@GrigorenkoPV
Contributor

@rfcbot concern impl-trait-syntax (well, I guess not enough rights)

Is there a reason why we use f: F ... where F: Fn... syntax instead of just f: impl Fn...? I recall having (or reading) a discussion about this with a reviewer like a year ago, and IIRC, they said that impl Fn is generally preferred, but since this syntax wasn't there since Rust 1.0, some APIs got stuck with an explicit generic argument.

Should I change this from

impl<T> Vec<T> {
    pub fn pop_if<F>(&mut self, f: F) -> Option<T>
        where F: FnOnce(&mut T) -> bool;
}

to

impl<T> Vec<T> {
    pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T>;
}

in stabilization PR (#135488)?

Amanieu commented on Jan 22, 2025

@Amanieu
Member

Yes, please do. It's always backwards-compatible to change an impl Trait to a generic parameter, but not the other way around.

2 remaining items

added
final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
and removed
proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
on Jan 29, 2025

rfcbot commented on Jan 29, 2025

@rfcbot

🔔 This is now entering its final comment period, as per the review above. 🔔

added and removed
final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
on Feb 8, 2025

rfcbot commented on Feb 8, 2025

@rfcbot

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

added a commit that references this issue on Feb 9, 2025
added a commit that references this issue on Feb 9, 2025
added a commit that references this issue on Feb 10, 2025
added 2 commits that reference this issue on Mar 11, 2025
added this to the 1.86.0 milestone on Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions