about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-05-29 15:07:11 +0900
committerGitHub <noreply@github.com>2020-05-29 15:07:11 +0900
commitfb506af138c40ebc3a62ada931259d60a74a6266 (patch)
tree345a3179c5724b40d43e6cdd9fa7172670f2e647
parent5207428176e0b78e5a57866b33d9e5aca7b9b112 (diff)
parent85f4f1c95a3eceae1e69a0fbf995cb7e7737e104 (diff)
downloadrust-fb506af138c40ebc3a62ada931259d60a74a6266.tar.gz
rust-fb506af138c40ebc3a62ada931259d60a74a6266.zip
Rollup merge of #72720 - poliorcetics:clarify-take-doc, r=joshtriplett
Clarify the documentation of `take`

This PR addresses the concerns of #61222, adding an example for the behaviour of `Iterator::take` when there are less than `n` elements.
-rw-r--r--src/libcore/iter/traits/iterator.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs
index 1c3d95cbb8c..7f081f732fd 100644
--- a/src/libcore/iter/traits/iterator.rs
+++ b/src/libcore/iter/traits/iterator.rs
@@ -1180,6 +1180,17 @@ pub trait Iterator {
     /// assert_eq!(iter.next(), Some(2));
     /// assert_eq!(iter.next(), None);
     /// ```
+    ///
+    /// If less than `n` elements are available,
+    /// `take` will limit itself to the size of the underlying iterator:
+    ///
+    /// ```
+    /// let v = vec![1, 2];
+    /// let mut iter = v.into_iter().take(5);
+    /// assert_eq!(iter.next(), Some(1));
+    /// assert_eq!(iter.next(), Some(2));
+    /// assert_eq!(iter.next(), None);
+    /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     fn take(self, n: usize) -> Take<Self>