Skip to content

Commit 0b7d68a

Browse files
committed
Fix core::iter::Fuse's Default impl to do what it's docs say it does.
Add a doctest with a non-empty-by-default iterator.
1 parent aae43c4 commit 0b7d68a

File tree

1 file changed

+20
-1
lines changed
  • library/core/src/iter/adapters

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,27 @@ impl<I: Default> Default for Fuse<I> {
198198
/// let iter: Fuse<slice::Iter<'_, u8>> = Default::default();
199199
/// assert_eq!(iter.len(), 0);
200200
/// ```
201+
///
202+
/// Note that if `I`'s default value is not an empty iterator, then this will not be
203+
/// an empty iterator.
204+
///
205+
/// ```
206+
/// # use std::iter::Fuse;
207+
/// #[derive(Default)]
208+
/// struct Fourever;
209+
///
210+
/// impl Iterator for Fourever {
211+
/// type Item = u32;
212+
/// fn next(&mut self) -> Option<u32> {
213+
/// Some(4)
214+
/// }
215+
/// }
216+
///
217+
/// let mut iter: Fuse<Fourever> = Default::default()
218+
/// assert_eq!(iter.next(), Some(4));
219+
/// ```
201220
fn default() -> Self {
202-
Fuse { iter: Default::default() }
221+
Fuse { iter: Some(I::default()) }
203222
}
204223
}
205224

0 commit comments

Comments
 (0)