about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-11 03:39:53 +0800
committerkennytm <kennytm@gmail.com>2018-02-11 03:39:53 +0800
commit4a827188cc46d47f75c72dba3a268d5c9f206582 (patch)
tree60faf17ac0f398109b52b4a99f9094731b7f7bb3 /src/libcore
parentb37c60276a8a679683f1ba717d7c72f83167fb80 (diff)
parentf129374d11d51ca23d85bc678fbc1ed4e7082ab1 (diff)
downloadrust-4a827188cc46d47f75c72dba3a268d5c9f206582.tar.gz
rust-4a827188cc46d47f75c72dba3a268d5c9f206582.zip
Rollup merge of #47547 - varkor:infinite-iterators-warning-doc, r=frewsxcv
Document the behaviour of infinite iterators on potentially-computable methods

It’s not entirely clear from the current documentation what behaviour
calling a method such as `min` on an infinite iterator like `RangeFrom`
is. One might expect this to terminate, but in fact, for infinite
iterators, `min` is always nonterminating (at least in the standard
library). This adds a quick note about this behaviour for clarification.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/iter/iterator.rs4
-rw-r--r--src/libcore/iter/mod.rs14
2 files changed, 18 insertions, 0 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs
index 296fb8733ba..33adb3f49dd 100644
--- a/src/libcore/iter/iterator.rs
+++ b/src/libcore/iter/iterator.rs
@@ -1431,6 +1431,10 @@ pub trait Iterator {
     /// Folding is useful whenever you have a collection of something, and want
     /// to produce a single value from it.
     ///
+    /// Note: `fold()`, and similar methods that traverse the entire iterator,
+    /// may not terminate for infinite iterators, even on traits for which a
+    /// result is determinable in finite time.
+    ///
     /// # Examples
     ///
     /// Basic usage:
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs
index bf8367d85fd..29b62c901f3 100644
--- a/src/libcore/iter/mod.rs
+++ b/src/libcore/iter/mod.rs
@@ -298,7 +298,21 @@
 //!
 //! This will print the numbers `0` through `4`, each on their own line.
 //!
+//! Bear in mind that methods on infinite iterators, even those for which a
+//! result can be determined mathematically in finite time, may not terminate.
+//! Specifically, methods such as [`min`], which in the general case require
+//! traversing every element in the iterator, are likely not to return
+//! successfully for any infinite iterators.
+//!
+//! ```no_run
+//! let ones = std::iter::repeat(1);
+//! let least = ones.min().unwrap(); // Oh no! An infinite loop!
+//! // `ones.min()` causes an infinite loop, so we won't reach this point!
+//! println!("The smallest number one is {}.", least);
+//! ```
+//!
 //! [`take`]: trait.Iterator.html#method.take
+//! [`min`]: trait.Iterator.html#method.min
 
 #![stable(feature = "rust1", since = "1.0.0")]