Skip to content

Commit 830ec99

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

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
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+
/// #![feature(peek_map)]
14+
///
15+
/// let a = [1, 2, 3];
16+
/// let iter = a.into_iter().peekable().peek_map(|x, next| x * *next.unwrap_or(&1));
17+
///
18+
/// assert_eq!(iter.next(), Some(2));
19+
/// assert_eq!(iter.next(), Some(6));
20+
/// assert_eq!(iter.next(), Some(3));
21+
/// assert_eq!(iter.next(), None);
22+
/// ```
423
#[must_use = "iterators are lazy and do nothing unless consumed"]
524
#[unstable(feature = "peek_map", issue = "118474")]
625
#[derive(Debug)]
7-
pub struct PeekMap<P, F> {
8-
pub(crate) peekable: P,
26+
pub struct PeekMap<T, F> {
27+
pub(crate) t: T,
928
f: F,
1029
}
1130

1231
impl<I: Iterator, F> PeekMap<Peekable<I>, F> {
1332
pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<Peekable<I>, F> {
14-
PeekMap { peekable, f }
33+
PeekMap { t: peekable, f }
1534
}
1635
}
1736

@@ -24,12 +43,12 @@ where
2443

2544
#[inline]
2645
fn next(&mut self) -> Option<B> {
27-
Some((&mut self.f)(self.peekable.next()?, self.peekable.peek()))
46+
Some((&mut self.f)(self.t.next()?, self.t.peek()))
2847
}
2948

3049
#[inline]
3150
fn size_hint(&self) -> (usize, Option<usize>) {
32-
self.peekable.size_hint()
51+
self.t.size_hint()
3352
}
3453
}
3554
#[unstable(feature = "peek_map", issue = "118474")]
@@ -38,11 +57,11 @@ where
3857
F: FnMut(I::Item, Option<&I::Item>) -> B,
3958
{
4059
fn len(&self) -> usize {
41-
self.peekable.len()
60+
self.t.len()
4261
}
4362

4463
fn is_empty(&self) -> bool {
45-
self.peekable.is_empty()
64+
self.t.is_empty()
4665
}
4766
}
4867
#[unstable(feature = "peek_map", issue = "118474")]

0 commit comments

Comments
 (0)