@@ -91,7 +91,7 @@ mod as_keyword {}
91
91
///
92
92
/// When associated with `loop`, a break expression may be used to return a value from that loop.
93
93
/// This is only valid with `loop` and not with any other type of loop.
94
- /// If no value is specified, `break;` returns `()`.
94
+ /// If no value is specified for `break;` it returns `()`.
95
95
/// Every `break` within a loop must return the same type.
96
96
///
97
97
/// ```rust
@@ -109,6 +109,33 @@ mod as_keyword {}
109
109
/// println!("{result}");
110
110
/// ```
111
111
///
112
+ /// It is also possible to exit from any *labelled* block returning the value early.
113
+ /// If no value is specified for `break;` it returns `()`.
114
+ ///
115
+ /// ```rust
116
+ /// let inputs = vec!["Cow", "Cat", "Dog", "Snake", "Cod"];
117
+ ///
118
+ /// let mut results = vec![];
119
+ /// for input in inputs {
120
+ /// let result = 'filter: {
121
+ /// if input.len() > 3 {
122
+ /// break 'filter Err("Too long");
123
+ /// };
124
+ ///
125
+ /// if !input.contains("C") {
126
+ /// break 'filter Err("No Cs");
127
+ /// };
128
+ ///
129
+ /// Ok(input.to_uppercase())
130
+ /// };
131
+ ///
132
+ /// results.push(result);
133
+ /// }
134
+ ///
135
+ /// // [Ok("COW"), Ok("CAT"), Err("No Cs"), Err("Too long"), Ok("COD")]
136
+ /// println!("{:?}", results)
137
+ /// ```
138
+ ///
112
139
/// For more details consult the [Reference on "break expression"] and the [Reference on "break and
113
140
/// loop values"].
114
141
///
0 commit comments