From 758ca9dc3aef53218f49ba0429379c66f06c6cbb Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Wed, 21 Sep 2022 17:07:50 -0400 Subject: [PATCH 1/3] Add examples to `bool::then` and `bool::then_some` Added examples to `bool::then` and `bool::then_some` to show the distinction between the eager evaluation of `bool::then_some` and the lazy evaluation of `bool::then`. --- library/core/src/bool.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 7667a650837..bf020bdba76 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -18,6 +18,18 @@ impl bool { /// assert_eq!(false.then_some(0), None); /// assert_eq!(true.then_some(0), Some(0)); /// ``` + /// + /// ``` + /// let mut a = 0; + /// let mut function_with_side_effects = || { a += 1; }; + /// + /// true.then_some(function_with_side_effects()); + /// false.then_some(function_with_side_effects()); + /// + /// // `a` is incremented twice because the value passed to `then_some` is + /// // evaluated eagerly. + /// assert_eq!(a, 2); + /// ``` #[stable(feature = "bool_to_option", since = "1.62.0")] #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")] #[inline] @@ -37,6 +49,16 @@ impl bool { /// assert_eq!(false.then(|| 0), None); /// assert_eq!(true.then(|| 0), Some(0)); /// ``` + /// + /// ``` + /// let mut a = 0; + /// + /// true.then(|| { a += 1; }); + /// false.then(|| { a += 1; }); + /// + /// // `a` is incremented once because the closure is evaluated lazily by + /// // `then`. + /// ``` #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")] #[inline] From 804cd8499b197907ee2c74c5f2c6dab57269d728 Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Wed, 21 Sep 2022 23:23:14 -0400 Subject: [PATCH 2/3] Remove trailing whitespace Trailing whitespace seemed to be causing the CI checks to error out. --- library/core/src/bool.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index bf020bdba76..e717d1f3524 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -18,16 +18,16 @@ impl bool { /// assert_eq!(false.then_some(0), None); /// assert_eq!(true.then_some(0), Some(0)); /// ``` - /// + /// /// ``` /// let mut a = 0; /// let mut function_with_side_effects = || { a += 1; }; - /// + /// /// true.then_some(function_with_side_effects()); /// false.then_some(function_with_side_effects()); - /// - /// // `a` is incremented twice because the value passed to `then_some` is - /// // evaluated eagerly. + /// + /// // `a` is incremented twice because the value passed to `then_some` is + /// // evaluated eagerly. /// assert_eq!(a, 2); /// ``` #[stable(feature = "bool_to_option", since = "1.62.0")] @@ -49,14 +49,14 @@ impl bool { /// assert_eq!(false.then(|| 0), None); /// assert_eq!(true.then(|| 0), Some(0)); /// ``` - /// + /// /// ``` /// let mut a = 0; - /// + /// /// true.then(|| { a += 1; }); /// false.then(|| { a += 1; }); - /// - /// // `a` is incremented once because the closure is evaluated lazily by + /// + /// // `a` is incremented once because the closure is evaluated lazily by /// // `then`. /// ``` #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] From ca26dec15fb35f8e27c517baf2b147edefbb4cd6 Mon Sep 17 00:00:00 2001 From: Venus Xeon-Blonde Date: Thu, 22 Sep 2022 02:12:06 -0400 Subject: [PATCH 3/3] Add missing assertion --- library/core/src/bool.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index e717d1f3524..db1c505ba38 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -58,6 +58,7 @@ impl bool { /// /// // `a` is incremented once because the closure is evaluated lazily by /// // `then`. + /// assert_eq!(a, 1); /// ``` #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]