summaryrefslogtreecommitdiffstats
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
parentAuto merge of #51144 - tmccombs:unix-epoch-stable, r=Mark-Simulacrum (diff)
parentDocument additional use case for iter::inspect (diff)
downloadgrust-4f6d9bf2099c83f365bb0b3cf02091b4737c2052.tar.gz
grust-4f6d9bf2099c83f365bb0b3cf02091b4737c2052.tar.bz2
grust-4f6d9bf2099c83f365bb0b3cf02091b4737c2052.tar.xz
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 d22c537621..f5b23a3793 100644
--- a/src/libcore/iter/iterator.rs
+++ b/src/libcore/iter/iterator.rs
@@ -1169,8 +1169,9 @@ pub trait Iterator {
1169 /// happening at various parts in the pipeline. To do that, insert 1169 /// happening at various parts in the pipeline. To do that, insert
1170 /// a call to `inspect()`. 1170 /// a call to `inspect()`.
1171 /// 1171 ///
1172 /// It's much more common for `inspect()` to be used as a debugging tool 1172 /// It's more common for `inspect()` to be used as a debugging tool than to
1173 /// than to exist in your final code, but never say never. 1173 /// exist in your final code, but applications may find it useful in certain
1174 /// situations when errors need to be logged before being discarded.
1174 /// 1175 ///
1175 /// # Examples 1176 /// # Examples
1176 /// 1177 ///
@@ -1210,6 +1211,32 @@ pub trait Iterator {
1210 /// about to filter: 3 1211 /// about to filter: 3
1211 /// 6 1212 /// 6
1212 /// ``` 1213 /// ```
1214 ///
1215 /// Logging errors before discarding them:
1216 ///
1217 /// ```
1218 /// let lines = ["1", "2", "a"];
1219 ///
1220 /// let sum: i32 = lines
1221 /// .iter()
1222 /// .map(|line| line.parse::<i32>())
1223 /// .inspect(|num| {
1224 /// if let Err(ref e) = *num {
1225 /// println!("Parsing error: {}", e);
1226 /// }
1227 /// })
1228 /// .filter_map(Result::ok)
1229 /// .sum();
1230 ///
1231 /// println!("Sum: {}", sum);
1232 /// ```
1233 ///
1234 /// This will print:
1235 ///
1236 /// ```text
1237 /// Parsing error: invalid digit found in string
1238 /// Sum: 3
1239 /// ```
1213 #[inline] 1240 #[inline]
1214 #[stable(feature = "rust1", since = "1.0.0")] 1241 #[stable(feature = "rust1", since = "1.0.0")]
1215 fn inspect<F>(self, f: F) -> Inspect<Self, F> where 1242 fn inspect<F>(self, f: F) -> Inspect<Self, F> where