Skip to content

Commit ca49731

Browse files
committed
Add docs to core::iter::adapters::PeekMap
1 parent 3a1d045 commit ca49731

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
use crate::iter::{FusedIterator, Peekable};
22

33
/// An iterator that maps the values of `iter` with `f`.
4+
///
5+
/// This struct is created by the [`peek_map`] method on [`Iterator`]. See its
6+
/// documentation for more.
7+
///
8+
/// # Examples
9+
///
10+
/// Basic usage:
11+
///
12+
/// ```
13+
/// let a = [1, 2, 3];
14+
/// let iter = a.iter().peekable().peek_map(|&x, next| {
15+
/// x * next.unwrap_or(&1)
16+
/// });
17+
///
18+
/// assert_eq!(iter.collect::<Vec<_>>(), vec![2, 6, 3]);
19+
/// ```
420
#[must_use = "iterators are lazy and do nothing unless consumed"]
521
#[unstable(feature = "peek_map", issue = "118474")]
622
#[derive(Debug)]
7-
pub struct PeekMap<P, F> {
8-
pub(crate) peekable: P,
23+
pub struct PeekMap<T, F> {
24+
pub(crate) t: T,
925
f: F,
1026
}
1127

1228
impl<I: Iterator, F> PeekMap<Peekable<I>, F> {
1329
pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<Peekable<I>, F> {
14-
PeekMap { peekable, f }
30+
PeekMap { t: peekable, f }
1531
}
1632
}
1733

@@ -24,12 +40,12 @@ where
2440

2541
#[inline]
2642
fn next(&mut self) -> Option<B> {
27-
Some((&mut self.f)(self.peekable.next()?, self.peekable.peek()))
43+
Some((&mut self.f)(self.t.next()?, self.t.peek()))
2844
}
2945

3046
#[inline]
3147
fn size_hint(&self) -> (usize, Option<usize>) {
32-
self.peekable.size_hint()
48+
self.t.size_hint()
3349
}
3450
}
3551
#[unstable(feature = "peek_map", issue = "118474")]
@@ -38,11 +54,11 @@ where
3854
F: FnMut(I::Item, Option<&I::Item>) -> B,
3955
{
4056
fn len(&self) -> usize {
41-
self.peekable.len()
57+
self.t.len()
4258
}
4359

4460
fn is_empty(&self) -> bool {
45-
self.peekable.is_empty()
61+
self.t.is_empty()
4662
}
4763
}
4864
#[unstable(feature = "peek_map", issue = "118474")]

0 commit comments

Comments
 (0)