Skip to content

Make Vec::pop_if a bit more presentable #135956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2511,9 +2511,9 @@ impl<T, A: Allocator> Vec<T, A> {
}
}

/// Removes and returns the last element in a vector if the predicate
/// Removes and returns the last element from a vector if the predicate
/// returns `true`, or [`None`] if the predicate returns false or the vector
/// is empty.
/// is empty (the predicate will not be called in that case).
///
/// # Examples
///
Expand All @@ -2528,12 +2528,9 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert_eq!(vec.pop_if(pred), None);
/// ```
#[unstable(feature = "vec_pop_if", issue = "122741")]
pub fn pop_if<F>(&mut self, f: F) -> Option<T>
where
F: FnOnce(&mut T) -> bool,
{
pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T> {
let last = self.last_mut()?;
if f(last) { self.pop() } else { None }
if predicate(last) { self.pop() } else { None }
}

/// Moves all the elements of `other` into `self`, leaving `other` empty.
Expand Down
Loading