-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add Peekable::next_if #72310
Add Peekable::next_if #72310
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1574,6 +1574,69 @@ impl<I: Iterator> Peekable<I> { | |
let iter = &mut self.iter; | ||
self.peeked.get_or_insert_with(|| iter.next()).as_ref() | ||
} | ||
|
||
/// Consume the next value of this iterator if a condition is true. | ||
/// | ||
/// If `func` returns `true` for the next value of this iterator, consume and return it. | ||
/// Otherwise, return `None`. | ||
/// | ||
/// # Examples | ||
/// Consume a number if it's equal to 0. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this description duplicated from the comments in the example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the same description, but using a different implementation. |
||
/// ``` | ||
/// #![feature(peekable_next_if)] | ||
/// let mut iter = (0..5).peekable(); | ||
/// // The first item of the iterator is 0; consume it. | ||
/// assert_eq!(iter.next_if(|&x| x == 0), Some(0)); | ||
/// // The next item returned is now 1, so `consume` will return `false`. | ||
/// assert_eq!(iter.next_if(|&x| x == 0), None); | ||
/// // `next_if` saves the value of the next item if it was not equal to `expected`. | ||
/// assert_eq!(iter.next(), Some(1)); | ||
/// ``` | ||
/// | ||
/// Consume any number less than 10. | ||
/// ``` | ||
/// #![feature(peekable_next_if)] | ||
/// let mut iter = (1..20).peekable(); | ||
/// // Consume all numbers less than 10 | ||
/// while iter.next_if(|&x| x < 10).is_some() {} | ||
/// // The next value returned will be 10 | ||
/// assert_eq!(iter.next(), Some(10)); | ||
/// ``` | ||
#[unstable(feature = "peekable_next_if", issue = "72480")] | ||
pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> { | ||
match self.next() { | ||
Some(matched) if func(&matched) => Some(matched), | ||
other => { | ||
// Since we called `self.next()`, we consumed `self.peeked`. | ||
assert!(self.peeked.is_none()); | ||
self.peeked = Some(other); | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// Consume the next item if it is equal to `expected`. | ||
/// | ||
/// # Example | ||
/// Consume a number if it's equal to 0. | ||
/// ``` | ||
/// #![feature(peekable_next_if)] | ||
/// let mut iter = (0..5).peekable(); | ||
/// // The first item of the iterator is 0; consume it. | ||
/// assert_eq!(iter.next_if_eq(&0), Some(0)); | ||
/// // The next item returned is now 1, so `consume` will return `false`. | ||
/// assert_eq!(iter.next_if_eq(&0), None); | ||
/// // `next_if_eq` saves the value of the next item if it was not equal to `expected`. | ||
/// assert_eq!(iter.next(), Some(1)); | ||
/// ``` | ||
#[unstable(feature = "peekable_next_if", issue = "72480")] | ||
pub fn next_if_eq<R>(&mut self, expected: &R) -> Option<I::Item> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why this is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think R for reference? I don't remember any more, I'll switch it to T. |
||
where | ||
R: ?Sized, | ||
I::Item: PartialEq<R>, | ||
{ | ||
self.next_if(|next| next == expected) | ||
} | ||
} | ||
|
||
/// An iterator that rejects elements while `predicate` returns `true`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
Consume and return
orTake
may be a better choice?