Skip to content

Tracking issue for std::iter::Peekable::peek_mut #78302

Closed
@lukaslueg

Description

@lukaslueg

Feature name: peekable_peek_mut

History:


A "peekable" iterator has a peek()-method which provides an immutable reference to the next item. We currently do not have a method to modify that item, which we could easily add via a peek_mut(). A draft-implementation to add a new peek_mut-method is in #77491

A peek_mut would allow one to peek into the very next item the iterator will return and change it if so desired (essentially a single-shot .map()):

let xs = vec![1, 2, 3];
let mut it = xs.into_iter().peekable();
if let Some(ref mut p) = it.peek_mut() {
    if **p == 1 {
        **p = 5;
    }
}
assert_eq!(it.collect::<Vec<_>>(), vec![5, 2, 3]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-TrackedLibs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions