Text of rust.docs.kernel.org › next › src › core › ops › control_flow.rs.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

control_flow.rs - source

Skip to main content core/ops/ control_flow.rs 1 use crate ::marker::Destruct ; 2 use crate ::ops ; 3 4 /// Used to tell an operation whether it should exit early or go on as usual. 5 /// 6 /// This is used when exposing things (like graph traversals or visitors) where 7 /// you want the user to be able to choose whether to exit early. 8 /// Having the enum makes it clearer -- no more wondering "wait, what did `false` 9 /// mean again?" -- and allows including a value.

10 /// 11 /// Similar to [`Option`] and [`Result`], this enum can be used with the `?` operator 12 /// to return immediately if the [`Break`] variant is present or otherwise continue normally 13 /// with the value inside the [`Continue`] variant. 14 /// 15 /// # Examples 16 /// 17 /// Early-exiting from [`Iterator::try_for_each`]: 18 /// ``` 19 /// use std::ops::ControlFlow; 20 /// 21 /// let r = (2..100).try_for_each(|x| { 22 /// if 403 % x == 0 { 23 /// return ControlFlow::Break(x) 24 /// } 25 /// 26 /// ControlFlow::Continue(()) 27 /// }); 28 /// assert_eq!(r, ControlFlow::Break(13)); 29 /// ``` 30 /// 31 /// A basic tree traversal: 32 /// ``` 33 /// use std::ops::ControlFlow; 34 /// 35 /// pub struct TreeNode<T> { 36 /// value: T, 37 /// left: Option<Box<TreeNode<T>>>, 38 /// right: Option<Box<TreeNode<T>>>, 39 /// } 40 /// 41 /// impl<T> TreeNode<T> { 42 /// pub fn traverse_inorder<B>(&self, f: &mut impl FnMut(&T) -> ControlFlow<B>) -> ControlFlow<B> { 43 /// if let Some(left) = &self.left { 44 /// left.traverse_inorder(f)?; 45 /// } 46 /// f(&self.value)?; 47 /// if let Some(right) = &self.right { 48 /// right.traverse_inorder(f)?; 49 /// } 50 /// ControlFlow::Continue(()) 51 /// } 52 /// fn leaf(value: T) -> Option<Box<TreeNode<T>>> { 53 /// Some(Box::new(Self { value, left: None, right: None })) 54 /// } 55 /// } 56 /// 57 /// let node = TreeNode { 58 /// value: 0, 59 /// left: TreeNode::leaf(1), 60 /// right: Some(Box::new(TreeNode { 61 /// value: -1, 62 /// left: TreeNode::leaf(5), 63 /// right: TreeNode::leaf(2), 64 /// })) 65 /// }; 66 /// let mut sum = 0; 67 /// 68 /// let res = node.traverse_inorder(&mut |val| { 69 /// if *val < 0 { 70 /// ControlFlow::Break(*val) 71 /// } else { 72 /// sum += *val; 73 /// ControlFlow::Continue(()) 74 /// } 75 /// }); 76 /// assert_eq!(res, ControlFlow::Break(-1)); 77 /// assert_eq!(sum, 6); 78 /// ``` 79 /// 80 /// [`Break`]: ControlFlow::Break 81 /// [`Continue`]: ControlFlow::Continue 82 #[stable(feature = "control_flow_enum_type" , since = "1.55.0" )] 83 #[rustc_diagnostic_item = "ControlFlow" ] 84 #[must_use] 85 // ControlFlow should not implement PartialOrd or Ord, per RFC 3058: 86 // https://rust-lang.github.io/rfcs/3058-try-trait-v2.html#traits-for-controlflow 87 #[derive( Copy , Debug , Hash )] 88 #[derive_const( Clone , PartialEq , Eq )] 89 pub enum ControlFlow <B, C = ()> { 90 /// Move on to the next phase of the operation as normal.

91 #[stable(feature = "control_flow_enum_type" , since = "1.55.0" )] 92 #[lang = "Continue" ] 93 Continue(C), 94 /// Exit the operation without running subsequent phases. 95 #[stable(feature = "control_flow_enum_type" , since = "1.55.0" )] 96 #[lang = "Break" ] 97 Break(B), 98 // Yes, the order of the variants doesn't match the type parameters. 99 // They're in this order so that `ControlFlow<A, B>` <-> `Result<B, A>` 100 // is a no-op conversion in the `Try` implementation.

101 } 102 103 #[unstable(feature = "try_trait_v2" , issue = "84277" , old_name = "try_trait" )] 104 #[rustc_const_unstable(feature = "const_try" , issue = "74935" )] 105 const impl <B, C> ops::Try for ControlFlow <B, C> { 106 type Output = C; 107 type Residual = ControlFlow <B, !>; 108 109 #[inline] 110 fn from_output(output: Self ::Output) -> Self { 111 ControlFlow:: Continue ( output ) 112 } 113 114 #[inline] 115 fn branch( self ) -> ControlFlow < Self ::Residual, Self ::Output> { 116 match self { 117 ControlFlow:: Continue (c) => ControlFlow:: Continue ( c ), 118 ControlFlow:: Break (b) => ControlFlow:: Break (ControlFlow:: Break ( b )), 119 } 120 } 121 } 122 123 #[unstable(feature = "try_trait_v2" , issue = "84277" , old_name = "try_trait" )] 124 #[rustc_const_unstable(feature = "const_try" , issue = "74935" )] 125 // Note: manually specifying the residual type instead of using the default to work around 126 // https://github.com/rust-lang/rust/issues/99940 127 const impl <B, C> ops::FromResidual < ControlFlow <B, !>> for ControlFlow <B, C> { 128 #[inline] 129 fn from_residual(residual: ControlFlow <B, !>) -> Self { 130 match residual { 131 ControlFlow:: Break (b) => ControlFlow:: Break ( b ), 132 } 133 } 134 } 135 136 #[unstable(feature = "try_trait_v2_residual" , issue = "91285" )] 137 #[rustc_const_unstable(feature = "const_try_residual" , issue = "91285" )] 138 const impl <B, C> ops::Residual <C> for ControlFlow <B, !> { 139 type TryType = ControlFlow <B, C>; 140 } 141 142 impl <B, C> ControlFlow <B, C> { 143 /// Returns `true` if this is a `Break` variant.

144 /// 145 /// # Examples 146 /// 147 /// ``` 148 /// use std::ops::ControlFlow; 149 /// 150 /// assert!(ControlFlow::<&str, i32>::Break("Stop right there!").is_break()); 151 /// assert!(!ControlFlow::<&str, i32>::Continue(3).is_break()); 152 /// ``` 153 #[inline] 154 #[stable(feature = "control_flow_enum_is" , since = "1.59.0" )] 155 #[rustc_const_stable(feature = "min_const_control_flow" , since = "1.95.0" )] 156 pub const fn is_break( & self ) -> bool { 157 matches! ( * self , ControlFlow::Break( _ )) 158 } 159 160 /// Returns `true` if this is a `Continue` variant.

161 /// 162 /// # Examples 163 /// 164 /// ``` 165 /// use std::ops::ControlFlow; 166 /// 167 /// assert!(!ControlFlow::<&str, i32>::Break("Stop right there!").is_continue()); 168 /// assert!(ControlFlow::<&str, i32>::Continue(3).is_continue()); 169 /// ``` 170 #[inline] 171 #[stable(feature = "control_flow_enum_is" , since = "1.59.0" )] 172 #[rustc_const_stable(feature = "min_const_control_flow" , since = "1.95.0" )] 173 pub const fn is_continue( & self ) -> bool { 174 matches! ( * self , ControlFlow::Continue( _ )) 175 } 176 177 /// Converts the `ControlFlow` into an `Option` which is `Some` if the 178 /// `ControlFlow` was `Break` and `None` otherwise.

179 /// 180 /// # Examples 181 /// 182 /// ``` 183 /// use std::ops::ControlFlow; 184 /// 185 /// assert_eq!(ControlFlow::<&str, i32>::Break("Stop right there!").break_value(), Some("Stop right there!")); 186 /// assert_eq!(ControlFlow::<&str, i32>::Continue(3).break_value(), None); 187 /// ``` 188 #[inline] 189 #[stable(feature = "control_flow_enum" , since = "1.83.0" )] 190 #[rustc_const_unstable(feature = "const_control_flow" , issue = "148739" )] 191 pub const fn break_value( self ) -> Option <B> 192 where 193 Self : [ const ] Destruct , 194 { 195 match self { 196 ControlFlow:: Continue (..) => None , 197 ControlFlow:: Break (x) => Some ( x ), 198 } 199 } 200 201 /// Converts the `ControlFlow` into a `Result` which is `Ok` if the 202 /// `ControlFlow` was `Break` and `Err` if otherwise.

203 /// 204 /// # Examples 205 /// 206 /// ``` 207 /// use std::ops::ControlFlow; 208 /// 209 /// struct TreeNode<T> { 210 /// value: T, 211 /// left: Option<Box<TreeNode<T>>>, 212 /// right: Option<Box<TreeNode<T>>>, 213 /// } 214 /// 215 /// impl<T> TreeNode<T> { 216 /// fn find<'a>(&'a self, mut predicate: impl FnMut(&T) -> bool) -> Result<&'a T, ()> { 217 /// let mut f = |t: &'a T| -> ControlFlow<&'a T> { 218 /// if predicate(t) { 219 /// ControlFlow::Break(t) 220 /// } else { 221 /// ControlFlow::Continue(()) 222 /// } 223 /// }; 224 /// 225 /// self.traverse_inorder(&mut f).break_ok() 226 /// } 227 /// 228 /// fn traverse_inorder<'a, B>( 229 /// &'a self, 230 /// f: &mut impl FnMut(&'a T) -> ControlFlow<B>, 231 /// ) -> ControlFlow<B> { 232 /// if let Some(left) = &self.left { 233 /// left.traverse_inorder(f)?; 234 /// } 235 /// f(&self.value)?; 236 /// if let Some(right) = &self.right { 237 /// right.traverse_inorder(f)?; 238 /// } 239 /// ControlFlow::Co

The rest of this page is on the live site.