@@ -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 >
0 commit comments