about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2019-03-20 13:08:54 +0100
committerFelix S. Klock II <pnkfelix@pnkfx.org>2019-03-22 12:44:08 +0100
commit48af7189c2f0e69c16611c261c4ded84c0ac305d (patch)
tree145d30b8dd642b3e8c2e4c98a9b8432032c4bcfb
parent0c8700b9d50a1e3d31f7b6c0956df555279ac441 (diff)
downloadrust-48af7189c2f0e69c16611c261c4ded84c0ac305d.tar.gz
rust-48af7189c2f0e69c16611c261c4ded84c0ac305d.zip
Expand `impl FromIterator for Option` doc to include example of early termination.
-rw-r--r--src/libcore/option.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index dfc388409a8..4fad65f3ae2 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -1315,6 +1315,26 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
     /// Since the last element is zero, it would underflow. Thus, the resulting
     /// value is `None`.
     ///
+    /// Here is a variation on the previous example, showing that no
+    /// further elements are taken from `iter` after the first `None`.
+    ///
+    /// ```
+    /// let items = vec![3_u16, 2, 1, 10];
+    ///
+    /// let mut shared = 0;
+    ///
+    /// let res: Option<Vec<u16>> = items
+    ///     .iter()
+    ///     .map(|x| shared += x; x.checked_sub(2))
+    ///     .collect();
+    ///
+    /// assert_eq!(res, None);
+    /// assert_eq!(shared, 6);
+    /// ```
+    ///
+    /// Since the third element caused an underflow, no further elements were taken,
+    /// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16.
+    ///
     /// [`Iterator`]: ../iter/trait.Iterator.html
     #[inline]
     fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {