@@ -798,6 +798,23 @@ impl<I, T, E> Iterator for ResultShunt<I, E>
798
798
impl < T , U , E > Sum < Result < U , E > > for Result < T , E >
799
799
where T : Sum < U > ,
800
800
{
801
+ /// Takes each element in the `Iterator`: if it is an `Err`, no further
802
+ /// elements are taken, and the `Err` is returned. Should no `Err` occur,
803
+ /// the sum of all elements is returned.
804
+ ///
805
+ /// # Examples
806
+ ///
807
+ /// This sums up every integer in a vector, rejecting the sum if a negative
808
+ /// element is encountered:
809
+ ///
810
+ /// ```
811
+ /// let v = vec![1, 2];
812
+ /// let res: Result<i32, &'static str> = v.iter().map(|&x: &i32|
813
+ /// if x < 0 { Err("Negative element found") }
814
+ /// else { Ok(x) }
815
+ /// ).sum();
816
+ /// assert_eq!(res, Ok(3));
817
+ /// ```
801
818
fn sum < I > ( iter : I ) -> Result < T , E >
802
819
where I : Iterator < Item = Result < U , E > > ,
803
820
{
@@ -809,6 +826,9 @@ impl<T, U, E> Sum<Result<U, E>> for Result<T, E>
809
826
impl < T , U , E > Product < Result < U , E > > for Result < T , E >
810
827
where T : Product < U > ,
811
828
{
829
+ /// Takes each element in the `Iterator`: if it is an `Err`, no further
830
+ /// elements are taken, and the `Err` is returned. Should no `Err` occur,
831
+ /// the product of all elements is returned.
812
832
fn product < I > ( iter : I ) -> Result < T , E >
813
833
where I : Iterator < Item = Result < U , E > > ,
814
834
{
@@ -819,7 +839,7 @@ impl<T, U, E> Product<Result<U, E>> for Result<T, E>
819
839
/// An iterator that always continues to yield `None` when exhausted.
820
840
///
821
841
/// Calling next on a fused iterator that has returned `None` once is guaranteed
822
- /// to return [`None`] again. This trait is should be implemented by all iterators
842
+ /// to return [`None`] again. This trait should be implemented by all iterators
823
843
/// that behave this way because it allows for some significant optimizations.
824
844
///
825
845
/// Note: In general, you should not use `FusedIterator` in generic bounds if
0 commit comments