about summary refs log tree commit diff
path: root/library/core/src/ops
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2020-09-04 16:28:23 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2020-09-04 16:28:23 -0700
commit59e37332b0d2d0c5e43bea1184ec0387fb07cc80 (patch)
treefc6596cfe62cd26c69ddc37acd580b186690a2c3 /library/core/src/ops
parentfac272688e9d561b1fff71621323682d23e4c31d (diff)
Add `BREAK` too, and improve the comments
Diffstat (limited to 'library/core/src/ops')
-rw-r--r--library/core/src/ops/control_flow.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/library/core/src/ops/control_flow.rs b/library/core/src/ops/control_flow.rs
index 3f0cce4e18a..b0c7dc1a518 100644
--- a/library/core/src/ops/control_flow.rs
+++ b/library/core/src/ops/control_flow.rs
@@ -69,6 +69,42 @@ impl<R: Try> ControlFlow<R::Ok, R> {
 impl<B> ControlFlow<(), B> {
     /// It's frequently the case that there's no value needed with `Continue`,
     /// so this provides a way to avoid typing `(())`, if you prefer it.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(control_flow_enum)]
+    /// use std::ops::ControlFlow;
+    ///
+    /// let mut partial_sum = 0;
+    /// let last_used = (1..10).chain(20..25).try_for_each(|x| {
+    ///     partial_sum += x;
+    ///     if partial_sum > 100 { ControlFlow::Break(x) }
+    ///     else { ControlFlow::CONTINUE }
+    /// });
+    /// assert_eq!(last_used.break_value(), Some(22));
+    /// ```
     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
     pub const CONTINUE: Self = ControlFlow::Continue(());
 }
+
+impl<C> ControlFlow<C, ()> {
+    /// APIs like `try_for_each` don't need values with `Break`,
+    /// so this provides a way to avoid typing `(())`, if you prefer it.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(control_flow_enum)]
+    /// use std::ops::ControlFlow;
+    ///
+    /// let mut partial_sum = 0;
+    /// (1..10).chain(20..25).try_for_each(|x| {
+    ///     if partial_sum > 100 { ControlFlow::BREAK }
+    ///     else { partial_sum += x; ControlFlow::CONTINUE }
+    /// });
+    /// assert_eq!(partial_sum, 108);
+    /// ```
+    #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
+    pub const BREAK: Self = ControlFlow::Break(());
+}