Skip to content

Commit f3d4278

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 f9c15f4 commit f3d4278

File tree

1 file changed

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

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,30 @@ 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+
/// This is equivalent to `I::default().fuse()`[^fuse_note]; e.g. if
203+
/// `I::default()` is not an empty iterator, then this will not be
204+
/// an empty iterator.
205+
///
206+
/// ```
207+
/// # use std::iter::Fuse;
208+
/// #[derive(Default)]
209+
/// struct Fourever;
210+
///
211+
/// impl Iterator for Fourever {
212+
/// type Item = u32;
213+
/// fn next(&mut self) -> Option<u32> {
214+
/// Some(4)
215+
/// }
216+
/// }
217+
///
218+
/// let mut iter: Fuse<Fourever> = Default::default();
219+
/// assert_eq!(iter.next(), Some(4));
220+
/// ```
221+
///
222+
/// [^fuse_note]: if `I` does not override `Iterator::fuse`'s default implementation
201223
fn default() -> Self {
202-
Fuse { iter: Default::default() }
224+
Fuse { iter: Some(I::default()) }
203225
}
204226
}
205227

0 commit comments

Comments
 (0)