diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs index 015cc150dc2..679cf3a9b23 100644 --- a/src/libcore/iter/traits.rs +++ b/src/libcore/iter/traits.rs @@ -798,6 +798,23 @@ impl Iterator for ResultShunt impl Sum> for Result where T: Sum, { + /// Takes each element in the `Iterator`: if it is an `Err`, no further + /// elements are taken, and the `Err` is returned. Should no `Err` occur, + /// the sum of all elements is returned. + /// + /// # Examples + /// + /// This sums up every integer in a vector, rejecting the sum if a negative + /// element is encountered: + /// + /// ``` + /// let v = vec![1, 2]; + /// let res: Result = v.iter().map(|&x: &i32| + /// if x < 0 { Err("Negative element found") } + /// else { Ok(x) } + /// ).sum(); + /// assert_eq!(res, Ok(3)); + /// ``` fn sum(iter: I) -> Result where I: Iterator>, { @@ -809,6 +826,9 @@ impl Sum> for Result impl Product> for Result where T: Product, { + /// Takes each element in the `Iterator`: if it is an `Err`, no further + /// elements are taken, and the `Err` is returned. Should no `Err` occur, + /// the product of all elements is returned. fn product(iter: I) -> Result where I: Iterator>, { @@ -819,7 +839,7 @@ impl Product> for Result /// An iterator that always continues to yield `None` when exhausted. /// /// Calling next on a fused iterator that has returned `None` once is guaranteed -/// to return [`None`] again. This trait is should be implemented by all iterators +/// to return [`None`] again. This trait should be implemented by all iterators /// that behave this way because it allows for some significant optimizations. /// /// Note: In general, you should not use `FusedIterator` in generic bounds if