Skip to content

Commit aa9c70b

Browse files
committed
Improve labeled blocks documentation
* list labeled blocks in the namespaces page * add an example * improve wording
1 parent ebab1cd commit aa9c70b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/expressions/loop-expr.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,27 @@ A `break` expression is only permitted in the body of a loop, and has one of the
249249
>    [_BlockExpression_]
250250
251251
Labelled block expressions are exactly like block expressions, except that they allow using `break` expressions within the block.
252-
Unlike other loops, `break` expressions within a label expression *must* have a label (i.e. the label is not optional).
253-
Unlike other loops, labelled block expressions *must* begin with a label.
252+
Unlike loops, `break` expressions within a labelled block expression *must* have a label (i.e. the label is not optional).
253+
Similarly, labelled block expressions *must* begin with a label.
254+
255+
```rust
256+
# fn do_thing() {}
257+
# fn condition_not_met() -> bool { true }
258+
# fn do_next_thing() {}
259+
# fn do_last_thing() {}
260+
let result = 'block: {
261+
do_thing();
262+
if condition_not_met() {
263+
break 'block 1;
264+
}
265+
do_next_thing();
266+
if condition_not_met() {
267+
break 'block 2;
268+
}
269+
do_last_thing();
270+
3
271+
};
272+
```
254273

255274
## `continue` expressions
256275

src/names/namespaces.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The following is a list of namespaces, with their corresponding entities:
5252
* [Generic lifetime parameters]
5353
* Label Namespace
5454
* [Loop labels]
55+
* [Block labels]
5556

5657
An example of how overlapping names in different namespaces can be used unambiguously:
5758

@@ -132,6 +133,7 @@ It is still an error for a [`use` import] to shadow another macro, regardless of
132133
[Attribute macros]: ../procedural-macros.md#attribute-macros
133134
[attributes]: ../attributes.md
134135
[bang-style macros]: ../macros.md
136+
[Block labels]: ../expressions/loop-expr.md#labelled-block-expressions
135137
[boolean]: ../types/boolean.md
136138
[Built-in attributes]: ../attributes.md#built-in-attributes-index
137139
[closure parameters]: ../expressions/closure-expr.md

0 commit comments

Comments
 (0)