rust-lang / rust-lang/rust

Tracking Issue for `PeekableIterator`

Open
#132,973 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Feature gate: #![feature(peekable_iterator)]

This is a tracking issue for the PeekableIterator trait, which extends Iterator with peek_with and related methods that inspect the next element without consuming it.

Public API
// core::iter

pub trait PeekableIterator: Iterator {
  // required method
  fn peek_with<T>(&mut self, func: impl for<'a> FnOnce(Option<&'a Self::Item>) -> T) -> T;

  // provided methods
  fn peek_map<T>(&mut self, func: impl for<'a> FnOnce(&'a Self::Item) -> T) -> Option<T>;
  fn next_if(&mut self, func: impl FnOnce(&Self::Item) -> bool) -> Option<Self::Item>;
  fn next_if_eq<T>(&mut self, expected: &T) -> Option<Self::Item>
  where
    Self::Item: PartialEq<T>,
    T: ?Sized;
}
Steps / History
  • ACP: rust-lang/libs-team#176
  • Implementation:
    • Take 1: #132976
    • Take 2: #144935
  • Final comment period (FCP)^1
  • Stabilization PR
Unresolved Questions
  • Should peek take &mut self or &self? &self makes sense for iterators such as core::slice::iter but precludes implementing the trait on Peekable.
  • What about the return type of peek? We could always make it return Self::Item like itertools’s PeekingNext, but that would prevent this trait from being implemented for consuming iterators such as vec::IntoIter, as well as Peekable itself. Resolved by having the method take a callback.
    • If we use an associated type, then what should be the bound for it: Borrow, AsRef, or something else?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the core::iter public API in this issue and read implementation attempts #132976 and #144935 before doing any work. The open questions about receiver and callback design still need resolution; completion would require implementation, the final comment period, and a stabilization PR.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.