Skip to content

Commit bec1ed7

Browse files
Document Iterator::take method behaviour
Also adds guarantee that `fn take()` doesn't take more than n elements.
1 parent 64df9e3 commit bec1ed7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

library/core/src/iter/traits/iterator.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,17 @@ pub trait Iterator {
13801380
/// assert_eq!(of_rust, vec!["of", "Rust"]);
13811381
/// ```
13821382
///
1383+
/// `take` would not call underlying iterator more than `n` times.
1384+
///
1385+
/// ```
1386+
/// let mut numbers = [1, 2, 3, 4, 5, 6, 7];
1387+
/// let mut inner = numbers.into_iter();
1388+
/// let a: Vec<_> = (&mut inner).take(3).collect();
1389+
/// let b: Vec<_> = inner.collect();
1390+
/// assert_eq!(a, [1, 2, 3]);
1391+
/// assert_eq!(a, [4, 5, 6, 7]);
1392+
/// ```
1393+
///
13831394
/// [`by_ref`]: Iterator::by_ref
13841395
#[doc(alias = "limit")]
13851396
#[inline]

0 commit comments

Comments
 (0)