Skip to content

Commit 08ec201

Browse files
committed
Expand docs on Peekable::peek_mut
1 parent 774bce7 commit 08ec201

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

+9-5
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,24 @@ impl<I: Iterator> Peekable<I> {
230230
///
231231
/// # Examples
232232
///
233+
/// Basic usage:
234+
///
233235
/// ```
234236
/// #![feature(peekable_peek_mut)]
235237
/// let mut iter = [1, 2, 3].iter().peekable();
236238
///
239+
/// // Like with `peek()`, we can see into the future without advancing the iterator.
240+
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
237241
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
238242
/// assert_eq!(iter.next(), Some(&1));
239243
///
240-
/// // Peek into the iterator and modify the value which will be returned next
241-
/// if let Some(mut p) = iter.peek_mut() {
242-
/// if *p == &2 {
243-
/// *p = &5;
244-
/// }
244+
/// // Peek into the iterator and set the value behind the mutable reference.
245+
/// if let Some(p) = iter.peek_mut() {
246+
/// assert_eq!(*p, &2);
247+
/// *p = &5;
245248
/// }
246249
///
250+
/// // The value we put in reappears as the iterator continues.
247251
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
248252
/// ```
249253
#[inline]

0 commit comments

Comments
 (0)