about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2025-05-02 22:17:01 +1000
committerGitHub <noreply@github.com>2025-05-02 22:17:01 +1000
commit30e556e7729d422bbe71db5ab28886429c4a950b (patch)
treec2c413426d9492a0768e6c468e7af3f882ee9e45
parent5a58c7a6ab060044399f7886cff330f3ee74c21c (diff)
parent175f71750f149643cff56f88b6f7e63c88843dea (diff)
downloadrust-30e556e7729d422bbe71db5ab28886429c4a950b.tar.gz
rust-30e556e7729d422bbe71db5ab28886429c4a950b.zip
Rollup merge of #140197 - ktnlvr:master, r=workingjubilee
Document breaking out of a named code block

Closes #110758.
-rw-r--r--library/std/src/keyword_docs.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/library/std/src/keyword_docs.rs b/library/std/src/keyword_docs.rs
index d715aca21a7..79b25040ef6 100644
--- a/library/std/src/keyword_docs.rs
+++ b/library/std/src/keyword_docs.rs
@@ -91,7 +91,7 @@ mod as_keyword {}
 ///
 /// When associated with `loop`, a break expression may be used to return a value from that loop.
 /// This is only valid with `loop` and not with any other type of loop.
-/// If no value is specified, `break;` returns `()`.
+/// If no value is specified for `break;` it returns `()`.
 /// Every `break` within a loop must return the same type.
 ///
 /// ```rust
@@ -109,6 +109,33 @@ mod as_keyword {}
 /// println!("{result}");
 /// ```
 ///
+/// It is also possible to exit from any *labelled* block returning the value early.
+/// If no value is specified for `break;` it returns `()`.
+///
+/// ```rust
+/// let inputs = vec!["Cow", "Cat", "Dog", "Snake", "Cod"];
+///
+/// let mut results = vec![];
+/// for input in inputs {
+///     let result = 'filter: {
+///         if input.len() > 3 {
+///             break 'filter Err("Too long");
+///         };
+///
+///         if !input.contains("C") {
+///             break 'filter Err("No Cs");
+///         };
+///
+///         Ok(input.to_uppercase())
+///     };
+///
+///     results.push(result);
+/// }
+///
+/// // [Ok("COW"), Ok("CAT"), Err("No Cs"), Err("Too long"), Ok("COD")]
+/// println!("{:?}", results)
+/// ```
+///
 /// For more details consult the [Reference on "break expression"] and the [Reference on "break and
 /// loop values"].
 ///