Skip to content

Commit abcbe54

Browse files
lukasluegKodrAus
andcommitted
Stabilize peekable_peek_mut
Resolves #78302 Update peekable.rs Update library/core/src/iter/traits/iterator.rs Co-authored-by: Ashley Mannix <[email protected]>
1 parent e423058 commit abcbe54

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

library/core/src/iter/adapters/peekable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ impl<I: Iterator> Peekable<I> {
233233
/// Basic usage:
234234
///
235235
/// ```
236-
/// #![feature(peekable_peek_mut)]
237236
/// let mut iter = [1, 2, 3].iter().peekable();
238237
///
239238
/// // Like with `peek()`, we can see into the future without advancing the iterator.
@@ -251,7 +250,7 @@ impl<I: Iterator> Peekable<I> {
251250
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
252251
/// ```
253252
#[inline]
254-
#[unstable(feature = "peekable_peek_mut", issue = "78302")]
253+
#[stable(feature = "peekable_peek_mut", since = "1.52.0")]
255254
pub fn peek_mut(&mut self) -> Option<&mut I::Item> {
256255
let iter = &mut self.iter;
257256
self.peeked.get_or_insert_with(|| iter.next()).as_mut()

library/core/src/iter/traits/iterator.rs

+34-12
Original file line numberDiff line numberDiff line change
@@ -936,20 +936,16 @@ pub trait Iterator {
936936
Enumerate::new(self)
937937
}
938938

939-
/// Creates an iterator which can use [`peek`] to look at the next element of
940-
/// the iterator without consuming it.
939+
/// Creates an iterator which can use the [`peek`] and [`peek_mut`] methods
940+
/// to look at the next element of the iterator without consuming it. See
941+
/// their documentation for more information.
941942
///
942-
/// Adds a [`peek`] method to an iterator. See its documentation for
943-
/// more information.
943+
/// Note that the underlying iterator is still advanced when [`peek`] or
944+
/// [`peek_mut`] are called for the first time: In order to retrieve the
945+
/// next element, [`next`] is called on the underlying iterator, hence any
946+
/// side effects (i.e. anything other than fetching the next value) of
947+
/// the [`next`] method will occur.
944948
///
945-
/// Note that the underlying iterator is still advanced when [`peek`] is
946-
/// called for the first time: In order to retrieve the next element,
947-
/// [`next`] is called on the underlying iterator, hence any side effects (i.e.
948-
/// anything other than fetching the next value) of the [`next`] method
949-
/// will occur.
950-
///
951-
/// [`peek`]: Peekable::peek
952-
/// [`next`]: Iterator::next
953949
///
954950
/// # Examples
955951
///
@@ -976,6 +972,32 @@ pub trait Iterator {
976972
/// assert_eq!(iter.peek(), None);
977973
/// assert_eq!(iter.next(), None);
978974
/// ```
975+
///
976+
/// Using [`peek_mut`] to mutate the next item without advancing the
977+
/// iterator:
978+
///
979+
/// ```
980+
/// let xs = [1, 2, 3];
981+
///
982+
/// let mut iter = xs.iter().peekable();
983+
///
984+
/// // peek_mut() lets us see into the future
985+
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
986+
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
987+
/// assert_eq!(iter.next(), Some(&1));
988+
///
989+
/// if let Some(mut p) = iter.peek_mut() {
990+
/// assert_eq!(*p, &2);
991+
/// // put a value into the iterator
992+
/// *p = &1000;
993+
/// }
994+
///
995+
/// // The value reappears as the iterator continues
996+
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&1000, &3]);
997+
/// ```
998+
/// [`peek`]: Peekable::peek
999+
/// [`peek_mut`]: Peekable::peek_mut
1000+
/// [`next`]: Iterator::next
9791001
#[inline]
9801002
#[stable(feature = "rust1", since = "1.0.0")]
9811003
fn peekable(self) -> Peekable<Self>

library/core/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
#![feature(unwrap_infallible)]
6868
#![feature(option_result_unwrap_unchecked)]
6969
#![feature(result_into_ok_or_err)]
70-
#![feature(peekable_peek_mut)]
7170
#![cfg_attr(not(bootstrap), feature(ptr_metadata))]
7271
#![feature(once_cell)]
7372
#![feature(unsized_tuple_coercion)]

0 commit comments

Comments
 (0)