Skip to content

Commit 1999153

Browse files
committed
Document breaking out of a named code block
1 parent 1f234b5 commit 1999153

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

std/src/keyword_docs.rs

+27
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,33 @@ mod as_keyword {}
109109
/// println!("{result}");
110110
/// ```
111111
///
112+
/// It is also possible to exit from any *labelled* block returning the value early.
113+
/// If no value specified `break;` 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+
///
112139
/// For more details consult the [Reference on "break expression"] and the [Reference on "break and
113140
/// loop values"].
114141
///

0 commit comments

Comments
 (0)