From 48af7189c2f0e69c16611c261c4ded84c0ac305d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 20 Mar 2019 13:08:54 +0100 Subject: [PATCH 1/4] Expand `impl FromIterator for Option` doc to include example of early termination. --- src/libcore/option.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index dfc388409a84b..4fad65f3ae28f 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1315,6 +1315,26 @@ impl> FromIterator> for Option { /// Since the last element is zero, it would underflow. Thus, the resulting /// value is `None`. /// + /// Here is a variation on the previous example, showing that no + /// further elements are taken from `iter` after the first `None`. + /// + /// ``` + /// let items = vec![3_u16, 2, 1, 10]; + /// + /// let mut shared = 0; + /// + /// let res: Option> = items + /// .iter() + /// .map(|x| shared += x; x.checked_sub(2)) + /// .collect(); + /// + /// assert_eq!(res, None); + /// assert_eq!(shared, 6); + /// ``` + /// + /// Since the third element caused an underflow, no further elements were taken, + /// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16. + /// /// [`Iterator`]: ../iter/trait.Iterator.html #[inline] fn from_iter>>(iter: I) -> Option { From d5a61c0be25d47c39ab7909b83b3a2765a282eb1 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 20 Mar 2019 13:09:22 +0100 Subject: [PATCH 2/4] Expand `impl FromIterator for Result` doc to include examples of `Err` and early termination. --- src/libcore/result.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 967f7e3e2fe72..490cd5bb3fe1d 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1202,6 +1202,34 @@ impl> FromIterator> for Result { /// ).collect(); /// assert_eq!(res, Ok(vec![2, 3])); /// ``` + /// + /// Here is another example that tries to subtract one from another list + /// of integers, this time checking for underflow: + /// + /// ``` + /// let v = vec![1, 2, 0]; + /// let res: Result, &'static str> = v.iter().map(|x: &u32| + /// x.checked_sub(1).ok_or("Underflow!") + /// ).collect(); + /// assert_eq!(res, Err("Underflow!")); + /// ``` + /// + /// Here is a variation on the previous example, showing that no + /// further elements are taken from `iter` after the first `Err`. + /// + /// ``` + /// let v = vec![3, 2, 1, 10]; + /// let mut shared = 0; + /// let res: Result, &'static str> = v.iter().map(|x: &u32| + /// shared += x; + /// x.checked_sub(2).ok_or("Underflow!") + /// ).collect(); + /// assert_eq!(res, Err("Underflow!")); + /// assert_eq!(shared, 6); + /// ``` + /// + /// Since the third element caused an underflow, no further elements were taken, + /// so the final value of `shared` is 6 (= `3 + 2 + 1`), not 16. #[inline] fn from_iter>>(iter: I) -> Result { // FIXME(#11084): This could be replaced with Iterator::scan when this From 6315221b39142a7ac2261c0f3afd93068fff2ac7 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Mon, 25 Mar 2019 11:48:08 +0100 Subject: [PATCH 3/4] Update src/libcore/option.rs Co-Authored-By: pnkfelix --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 4fad65f3ae28f..3da92c0a05ac4 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1325,7 +1325,7 @@ impl> FromIterator> for Option { /// /// let res: Option> = items /// .iter() - /// .map(|x| shared += x; x.checked_sub(2)) + /// .map(|x| { shared += x; x.checked_sub(2) }) /// .collect(); /// /// assert_eq!(res, None); From 0e83e96852e9aacde2bf633bb17b293910073812 Mon Sep 17 00:00:00 2001 From: Felix S Klock II Date: Mon, 25 Mar 2019 11:50:11 +0100 Subject: [PATCH 4/4] add missing braces add missing braces analogous to those suggested by killercup --- src/libcore/result.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 490cd5bb3fe1d..9b7b83689861b 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -1220,10 +1220,10 @@ impl> FromIterator> for Result { /// ``` /// let v = vec![3, 2, 1, 10]; /// let mut shared = 0; - /// let res: Result, &'static str> = v.iter().map(|x: &u32| + /// let res: Result, &'static str> = v.iter().map(|x: &u32| { /// shared += x; /// x.checked_sub(2).ok_or("Underflow!") - /// ).collect(); + /// }).collect(); /// assert_eq!(res, Err("Underflow!")); /// assert_eq!(shared, 6); /// ```