You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>{pubfnpeek_mut(&mutself) -> Option<PeekMut<'_,T>>;}pubstructPeekMut<'a,T>;impl<'a,T>PeekMut<'a,T>{pubfnpop(self) -> T;}// impl `Deref`/`DerefMut` for `PeekMut`
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:
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.
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".
Reacted by Ludwig Stecher, crumblingstatue, im-nefilim, Giovanni, Filipe Rodrigues, Ryan Avella and Ronno Das
Activity
GrigorenkoPV commentedon Mar 22, 2024
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
peekorpeek_mutfor two reasons:peekin name usually revolve aroundOption<&T>and not an Entry-style API similar to Map's Entry.Vec, similarly topushandpop, but I would argue thatpeeksounds closer toget, which is not tied to a specific position.I would personally prefer something along the lines of "
last_entry".Rollup merge of rust-lang#142046 - Qelxiros:122742-vec_peek_mut, r=cu…
Unrolled build for #142046
Rollup merge of rust-lang#142046 - Qelxiros:122742-vec_peek_mut, r=cu…
alex-700 commentedon Jul 11, 2025
The
BinaryHeap'sPeekMut::pophasthis-named parameter on puprose. See comment for the reason.Should we avoid the same problem here in
Vec'sPeekMut::pop?Switch `std::vec::PeekMut::pop` from self to this parameter.
Rollup merge of rust-lang#146546 - cammeresi:pop, r=Mark-Simulacrum
Rollup merge of rust-lang#146546 - cammeresi:pop, r=Mark-Simulacrum
Rollup merge of rust-lang#146546 - cammeresi:pop, r=Mark-Simulacrum
Unrolled build for #146546
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of rust-lang#146621 - cammeresi:peek-20250915, r=Amanieu
Rollup merge of rust-lang#146546 - cammeresi:pop, r=Mark-Simulacrum
Auto merge of rust-lang#146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of rust-lang#146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of rust-lang#146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu
Auto merge of #146621 - cammeresi:peek-20250915, r=Amanieu