Skip to content

Commit fb506af

Browse files
authored
Rollup merge of rust-lang#72720 - poliorcetics:clarify-take-doc, r=joshtriplett
Clarify the documentation of `take` This PR addresses the concerns of rust-lang#61222, adding an example for the behaviour of `Iterator::take` when there are less than `n` elements.
2 parents 5207428 + 85f4f1c commit fb506af

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libcore/iter/traits/iterator.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,17 @@ pub trait Iterator {
11801180
/// assert_eq!(iter.next(), Some(2));
11811181
/// assert_eq!(iter.next(), None);
11821182
/// ```
1183+
///
1184+
/// If less than `n` elements are available,
1185+
/// `take` will limit itself to the size of the underlying iterator:
1186+
///
1187+
/// ```
1188+
/// let v = vec![1, 2];
1189+
/// let mut iter = v.into_iter().take(5);
1190+
/// assert_eq!(iter.next(), Some(1));
1191+
/// assert_eq!(iter.next(), Some(2));
1192+
/// assert_eq!(iter.next(), None);
1193+
/// ```
11831194
#[inline]
11841195
#[stable(feature = "rust1", since = "1.0.0")]
11851196
fn take(self, n: usize) -> Take<Self>

0 commit comments

Comments
 (0)