From 36ae6575fd85dd6eb9a00e097e47b38fa25235a0 Mon Sep 17 00:00:00 2001 From: Artur Roos Date: Wed, 23 Apr 2025 14:31:34 +0300 Subject: [PATCH 1/2] Document breaking out of a named code block --- library/std/src/keyword_docs.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/library/std/src/keyword_docs.rs b/library/std/src/keyword_docs.rs index c07c391892d80..b0e55c787250d 100644 --- a/library/std/src/keyword_docs.rs +++ b/library/std/src/keyword_docs.rs @@ -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 specified `break;` 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"]. /// From 175f71750f149643cff56f88b6f7e63c88843dea Mon Sep 17 00:00:00 2001 From: Artur Roos Date: Thu, 1 May 2025 22:09:07 +0300 Subject: [PATCH 2/2] Simplify docs for breaking out of a named code block --- library/std/src/keyword_docs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/keyword_docs.rs b/library/std/src/keyword_docs.rs index b0e55c787250d..71b68233f7881 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 @@ -110,7 +110,7 @@ mod as_keyword {} /// ``` /// /// It is also possible to exit from any *labelled* block returning the value early. -/// If no value specified `break;` returns `()`. +/// If no value is specified for `break;` it returns `()`. /// /// ```rust /// let inputs = vec!["Cow", "Cat", "Dog", "Snake", "Cod"];