Tracking Issue for PeekableIterator
· Issue #132973 · rust-lang/rust (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Description
Feature gate: #![feature(peekable_iterator)]
This is a tracking issue for the PeekableIterator
trait, which extends Iterator
with peek
and related methods that inspect the next element without consuming it.
Public API
// core::iter
pub trait PeekableIterator: Iterator { type PeekedItem<'a>: BorrowSelf::Item + 'a where Self: 'a; fn peek(&self) -> Option<PeekedItem<'_>>; fn next_if(&mut self, func: impl FnOnce(&Self::Item) -> bool) -> OptionSelf::Item; fn next_if_eq(&mut self, expected: &T) -> OptionSelf::Item where Self::Item: PartialEq, T: ?Sized; }
Steps / History
- ACP: Peek trait for peekable iterators libs-team#176
- Implementation: Add PeekableIterator trait #132976
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Should
peek
take&mut self
or&self
?&self
makes sense for iterators such ascore::slice::iter
but precludes implementing the trait onPeekable
. - What about the return type of
peek
? We could always make it returnSelf::Item
like itertools’s PeekingNext, but that would prevent this trait from being implemented for consuming iterators such asvec::IntoIter
, as well asPeekable
itself.- If we use an associated type, then what should be the bound for it:
Borrow
,AsRef
, or something else?
- If we use an associated type, then what should be the bound for it: