Skip to content

Commit 5330e2c

Browse files
authored
Rollup merge of rust-lang#140197 - ktnlvr:master, r=workingjubilee
Document breaking out of a named code block Closes rust-lang#110758.
2 parents dd44b30 + 175f717 commit 5330e2c

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

library/std/src/keyword_docs.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod as_keyword {}
9191
///
9292
/// When associated with `loop`, a break expression may be used to return a value from that loop.
9393
/// 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 `()`.
9595
/// Every `break` within a loop must return the same type.
9696
///
9797
/// ```rust
@@ -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 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+
///
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)