Skip to content

Commit 1ef8491

Browse files
authored
Rollup merge of #72626 - phimuemue:doubleendediter_doc, r=dtolnay
Add remark regarding DoubleEndedIterator While reviewing rust-itertools/itertools@14293bd#diff-2c16d2ada06ad2fd1fc754679646d471, I realized that a `DoubleEndedIterator` may yield different elements depending on whether it is traversed forwards or backwards. (Not only the *order*, but possibly also the yielded values.) I found this remarkable, but could not find anything in the current docs, so I thought it may be worth mentioning this explicitly. Unfortunately, I could not test these changes locally (`rustdoc` complains about `unresolved import`). Sorry if this causes headache. If I should change something, please let me know. If it seems too trivial, feel free to just close this PR.
2 parents fc7afd1 + b60fe39 commit 1ef8491

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/libcore/iter/traits/double_ended.rs

+26
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ pub trait DoubleEndedIterator: Iterator {
6363
/// assert_eq!(None, iter.next());
6464
/// assert_eq!(None, iter.next_back());
6565
/// ```
66+
///
67+
/// # Remarks
68+
///
69+
/// The elements yielded by `DoubleEndedIterator`'s methods may differ from
70+
/// the ones yielded by `Iterator`'s methods:
71+
///
72+
/// ```
73+
/// let vec = vec![(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b')];
74+
/// let uniq_by_fst_comp = || {
75+
/// let mut seen = std::collections::HashSet::new();
76+
/// vec.iter().copied().filter(move |x| seen.insert(x.0))
77+
/// };
78+
///
79+
/// assert_eq!(uniq_by_fst_comp().last(), Some((2, 'a')));
80+
/// assert_eq!(uniq_by_fst_comp().next_back(), Some((2, 'b')));
81+
///
82+
/// assert_eq!(
83+
/// uniq_by_fst_comp().fold(vec![], |mut v, x| {v.push(x); v}),
84+
/// vec![(1, 'a'), (2, 'a')]
85+
/// );
86+
/// assert_eq!(
87+
/// uniq_by_fst_comp().rfold(vec![], |mut v, x| {v.push(x); v}),
88+
/// vec![(2, 'b'), (1, 'c')]
89+
/// );
90+
/// ```
91+
///
6692
#[stable(feature = "rust1", since = "1.0.0")]
6793
fn next_back(&mut self) -> Option<Self::Item>;
6894

0 commit comments

Comments
 (0)