Skip to content

Commit aa61b12

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

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
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 [`Peekable`]. See its
6+
/// documentation for more.
7+
///
8+
/// [`peek_map`]: Peekable::peek_map
9+
/// [`Peekable`]: struct.Peekable.html
10+
///
11+
/// # Examples
12+
///
13+
/// Basic usage:
14+
///
15+
/// ```
16+
/// #![feature(peek_map)]
17+
///
18+
/// let a = [1, 2, 3];
19+
/// let mut iter = a.into_iter().peekable().peek_map(|x, next| x * *next.unwrap_or(&1));
20+
///
21+
/// assert_eq!(iter.next(), Some(2));
22+
/// assert_eq!(iter.next(), Some(6));
23+
/// assert_eq!(iter.next(), Some(3));
24+
/// assert_eq!(iter.next(), None);
25+
/// ```
426
#[must_use = "iterators are lazy and do nothing unless consumed"]
527
#[unstable(feature = "peek_map", issue = "118474")]
628
#[derive(Debug)]
7-
pub struct PeekMap<P, F> {
8-
pub(crate) peekable: P,
29+
pub struct PeekMap<T, F> {
30+
pub(crate) t: T,
931
f: F,
1032
}
1133

1234
impl<I: Iterator, F> PeekMap<Peekable<I>, F> {
1335
pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<Peekable<I>, F> {
14-
PeekMap { peekable, f }
36+
PeekMap { t: peekable, f }
1537
}
1638
}
1739

@@ -24,12 +46,12 @@ where
2446

2547
#[inline]
2648
fn next(&mut self) -> Option<B> {
27-
Some((&mut self.f)(self.peekable.next()?, self.peekable.peek()))
49+
Some((&mut self.f)(self.t.next()?, self.t.peek()))
2850
}
2951

3052
#[inline]
3153
fn size_hint(&self) -> (usize, Option<usize>) {
32-
self.peekable.size_hint()
54+
self.t.size_hint()
3355
}
3456
}
3557
#[unstable(feature = "peek_map", issue = "118474")]
@@ -38,11 +60,11 @@ where
3860
F: FnMut(I::Item, Option<&I::Item>) -> B,
3961
{
4062
fn len(&self) -> usize {
41-
self.peekable.len()
63+
self.t.len()
4264
}
4365

4466
fn is_empty(&self) -> bool {
45-
self.peekable.is_empty()
67+
self.t.is_empty()
4668
}
4769
}
4870
#[unstable(feature = "peek_map", issue = "118474")]

0 commit comments

Comments
 (0)