Skip to content

Tracking Issue for Vec::peek_mut #122742

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

Open
1 of 4 tasks
avandesa opened this issue Mar 19, 2024 · 1 comment
Open
1 of 4 tasks

Tracking Issue for Vec::peek_mut #122742

avandesa opened this issue Mar 19, 2024 · 1 comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@avandesa
Copy link
Contributor

Feature gate: #![feature(vec_peek_mut)]

This feature adds Vec::peek_mut, which returns a PeekMut struct. It is analogous to [BinaryHeap::peek_mut] and enables users to conditionally modify and remove the last element of a Vec without having to call unwrap.

Public API

impl<T> Vec<T> {
    pub fn peek_mut(&mut self) -> Option<PeekMut<'_, T>>;
}

pub struct PeekMut<'a, T>;

impl<'a, T> PeekMut<'a, T> {
    pub fn pop(self) -> T;
}

// impl `Deref`/`DerefMut` for `PeekMut`

Steps / History

Unresolved Questions

  • Should this be named peek or peek_mut?

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@avandesa avandesa added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 19, 2024
@GrigorenkoPV
Copy link
Contributor

Even though FCP has not started yet, I would like to express a couple of concerns regarding the name. I am not very pleased with either peek or peek_mut for two reasons:

  1. All the other std APIs with peek in name usually revolve around Option<&T> and not an Entry-style API similar to Map's Entry.
  2. The name does not reflect which element it operates on. After some thinking you can probably guess that it means the last element for Vec, similarly to push and pop, but I would argue that peek sounds closer to get, which is not tied to a specific position.

I would personally prefer something along the lines of "last_entry".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants