about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-05-29 06:50:12 +0000
committerbors <bors@rust-lang.org>2018-05-29 06:50:12 +0000
commit4f6d9bf2099c83f365bb0b3cf02091b4737c2052 (patch)
tree610ea40a6c1e05c3e9dc7f23f4f00e24402bac7f /src
parentfe5c45bb53268e9496d36a6f5956a7ec6e4854b3 (diff)
parent18cf47bc7de897168d19806e13467d6c8168a581 (diff)
downloadrust-4f6d9bf2099c83f365bb0b3cf02091b4737c2052.tar.gz
rust-4f6d9bf2099c83f365bb0b3cf02091b4737c2052.zip
Auto merge of #51142 - nickbabcock:doc-inspect, r=frewsxcv
Document additional use case for iter::inspect

Adds docs for `iter::inspect` showing the non-debug use case

Closes #49564
Diffstat (limited to 'src')
-rw-r--r--src/libcore/iter/iterator.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/libcore/iter/iterator.rs b/src/libcore/iter/iterator.rs
index d22c5376211..f5b23a3793b 100644
--- a/src/libcore/iter/iterator.rs
+++ b/src/libcore/iter/iterator.rs
@@ -1169,8 +1169,9 @@ pub trait Iterator {
     /// happening at various parts in the pipeline. To do that, insert
     /// a call to `inspect()`.
     ///
-    /// It's much more common for `inspect()` to be used as a debugging tool
-    /// than to exist in your final code, but never say never.
+    /// It's more common for `inspect()` to be used as a debugging tool than to
+    /// exist in your final code, but applications may find it useful in certain
+    /// situations when errors need to be logged before being discarded.
     ///
     /// # Examples
     ///
@@ -1210,6 +1211,32 @@ pub trait Iterator {
     /// about to filter: 3
     /// 6
     /// ```
+    ///
+    /// Logging errors before discarding them:
+    ///
+    /// ```
+    /// let lines = ["1", "2", "a"];
+    ///
+    /// let sum: i32 = lines
+    ///     .iter()
+    ///     .map(|line| line.parse::<i32>())
+    ///     .inspect(|num| {
+    ///         if let Err(ref e) = *num {
+    ///             println!("Parsing error: {}", e);
+    ///         }
+    ///     })
+    ///     .filter_map(Result::ok)
+    ///     .sum();
+    ///
+    /// println!("Sum: {}", sum);
+    /// ```
+    ///
+    /// This will print:
+    ///
+    /// ```text
+    /// Parsing error: invalid digit found in string
+    /// Sum: 3
+    /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     fn inspect<F>(self, f: F) -> Inspect<Self, F> where