mirror of
https://github.com/rust-lang/rust
synced 2024-10-22 16:35:28 +00:00
Rollup merge of #81938 - lukaslueg:stab_peek_mut, r=Amanieu
Stabilize `peekable_peek_mut` Resolves #78302. Also adds some documentation on `std::iter::Iterator::peekable()` regarding the new method. The feature was added in #77491 in Nov' 20, which is recently, but the feature seems reasonably small. Never did a stabilization-pr, excuse my ignorance if there is a protocol I'm not aware of.
This commit is contained in:
commit
461297e3fd
|
@ -233,7 +233,6 @@ pub fn peek(&mut self) -> Option<&I::Item> {
|
|||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(peekable_peek_mut)]
|
||||
/// let mut iter = [1, 2, 3].iter().peekable();
|
||||
///
|
||||
/// // Like with `peek()`, we can see into the future without advancing the iterator.
|
||||
|
@ -251,7 +250,7 @@ pub fn peek(&mut self) -> Option<&I::Item> {
|
|||
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "peekable_peek_mut", issue = "78302")]
|
||||
#[stable(feature = "peekable_peek_mut", since = "1.53.0")]
|
||||
pub fn peek_mut(&mut self) -> Option<&mut I::Item> {
|
||||
let iter = &mut self.iter;
|
||||
self.peeked.get_or_insert_with(|| iter.next()).as_mut()
|
||||
|
|
|
@ -937,20 +937,16 @@ fn enumerate(self) -> Enumerate<Self>
|
|||
Enumerate::new(self)
|
||||
}
|
||||
|
||||
/// Creates an iterator which can use [`peek`] to look at the next element of
|
||||
/// the iterator without consuming it.
|
||||
/// Creates an iterator which can use the [`peek`] and [`peek_mut`] methods
|
||||
/// to look at the next element of the iterator without consuming it. See
|
||||
/// their documentation for more information.
|
||||
///
|
||||
/// Adds a [`peek`] method to an iterator. See its documentation for
|
||||
/// more information.
|
||||
/// Note that the underlying iterator is still advanced when [`peek`] or
|
||||
/// [`peek_mut`] are called for the first time: In order to retrieve the
|
||||
/// next element, [`next`] is called on the underlying iterator, hence any
|
||||
/// side effects (i.e. anything other than fetching the next value) of
|
||||
/// the [`next`] method will occur.
|
||||
///
|
||||
/// Note that the underlying iterator is still advanced when [`peek`] is
|
||||
/// called for the first time: In order to retrieve the next element,
|
||||
/// [`next`] is called on the underlying iterator, hence any side effects (i.e.
|
||||
/// anything other than fetching the next value) of the [`next`] method
|
||||
/// will occur.
|
||||
///
|
||||
/// [`peek`]: Peekable::peek
|
||||
/// [`next`]: Iterator::next
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -977,6 +973,32 @@ fn enumerate(self) -> Enumerate<Self>
|
|||
/// assert_eq!(iter.peek(), None);
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
///
|
||||
/// Using [`peek_mut`] to mutate the next item without advancing the
|
||||
/// iterator:
|
||||
///
|
||||
/// ```
|
||||
/// let xs = [1, 2, 3];
|
||||
///
|
||||
/// let mut iter = xs.iter().peekable();
|
||||
///
|
||||
/// // `peek_mut()` lets us see into the future
|
||||
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
|
||||
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
|
||||
/// assert_eq!(iter.next(), Some(&1));
|
||||
///
|
||||
/// if let Some(mut p) = iter.peek_mut() {
|
||||
/// assert_eq!(*p, &2);
|
||||
/// // put a value into the iterator
|
||||
/// *p = &1000;
|
||||
/// }
|
||||
///
|
||||
/// // The value reappears as the iterator continues
|
||||
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&1000, &3]);
|
||||
/// ```
|
||||
/// [`peek`]: Peekable::peek
|
||||
/// [`peek_mut`]: Peekable::peek_mut
|
||||
/// [`next`]: Iterator::next
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn peekable(self) -> Peekable<Self>
|
||||
|
|
|
@ -66,7 +66,6 @@
|
|||
#![feature(unwrap_infallible)]
|
||||
#![feature(option_result_unwrap_unchecked)]
|
||||
#![feature(result_into_ok_or_err)]
|
||||
#![feature(peekable_peek_mut)]
|
||||
#![feature(ptr_metadata)]
|
||||
#![feature(once_cell)]
|
||||
#![feature(unsized_tuple_coercion)]
|
||||
|
|
Loading…
Reference in a new issue