Skip to content

Commit 54240b6

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

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
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.into_iter().peekable().peek_map(|x, next| x * *next.unwrap_or(&1));
15+
///
16+
/// assert_eq!(iter.next(), Some(2));
17+
/// assert_eq!(iter.next(), Some(6));
18+
/// assert_eq!(iter.next(), Some(3));
19+
/// assert_eq!(iter.next(), None);
20+
/// ```
421
#[must_use = "iterators are lazy and do nothing unless consumed"]
522
#[unstable(feature = "peek_map", issue = "118474")]
623
#[derive(Debug)]
7-
pub struct PeekMap<P, F> {
8-
pub(crate) peekable: P,
24+
pub struct PeekMap<T, F> {
25+
pub(crate) t: T,
926
f: F,
1027
}
1128

1229
impl<I: Iterator, F> PeekMap<Peekable<I>, F> {
1330
pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<Peekable<I>, F> {
14-
PeekMap { peekable, f }
31+
PeekMap { t: peekable, f }
1532
}
1633
}
1734

@@ -24,12 +41,12 @@ where
2441

2542
#[inline]
2643
fn next(&mut self) -> Option<B> {
27-
Some((&mut self.f)(self.peekable.next()?, self.peekable.peek()))
44+
Some((&mut self.f)(self.t.next()?, self.t.peek()))
2845
}
2946

3047
#[inline]
3148
fn size_hint(&self) -> (usize, Option<usize>) {
32-
self.peekable.size_hint()
49+
self.t.size_hint()
3350
}
3451
}
3552
#[unstable(feature = "peek_map", issue = "118474")]
@@ -38,11 +55,11 @@ where
3855
F: FnMut(I::Item, Option<&I::Item>) -> B,
3956
{
4057
fn len(&self) -> usize {
41-
self.peekable.len()
58+
self.t.len()
4259
}
4360

4461
fn is_empty(&self) -> bool {
45-
self.peekable.is_empty()
62+
self.t.is_empty()
4663
}
4764
}
4865
#[unstable(feature = "peek_map", issue = "118474")]

0 commit comments

Comments
 (0)