Skip to content

Commit 5aa3403

Browse files
committed
Auto merge of #42570 - birkenfeld:patch-3, r=frewsxcv
Add dedicated docstrings to Sum/Product impl of Result (and fix a minor grammar typo below)
2 parents 3f8b936 + 2366c46 commit 5aa3403

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/libcore/iter/traits.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,23 @@ impl<I, T, E> Iterator for ResultShunt<I, E>
798798
impl<T, U, E> Sum<Result<U, E>> for Result<T, E>
799799
where T: Sum<U>,
800800
{
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+
/// ```
801818
fn sum<I>(iter: I) -> Result<T, E>
802819
where I: Iterator<Item = Result<U, E>>,
803820
{
@@ -809,6 +826,9 @@ impl<T, U, E> Sum<Result<U, E>> for Result<T, E>
809826
impl<T, U, E> Product<Result<U, E>> for Result<T, E>
810827
where T: Product<U>,
811828
{
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.
812832
fn product<I>(iter: I) -> Result<T, E>
813833
where I: Iterator<Item = Result<U, E>>,
814834
{
@@ -819,7 +839,7 @@ impl<T, U, E> Product<Result<U, E>> for Result<T, E>
819839
/// An iterator that always continues to yield `None` when exhausted.
820840
///
821841
/// 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
823843
/// that behave this way because it allows for some significant optimizations.
824844
///
825845
/// Note: In general, you should not use `FusedIterator` in generic bounds if

0 commit comments

Comments
 (0)