Skip to content

Commit 2ea5bc8

Browse files
committed
add unstable book entry
1 parent 8ee6e7b commit 2ea5bc8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# `loop_match`
2+
3+
The tracking issue for this feature is: [#132306]
4+
5+
[#132306]: https://github.com/rust-lang/rust/issues/132306
6+
7+
------
8+
9+
The `#[loop_match]` and `#[const_continue]` attributes can be used to improve the code
10+
generation of logic that fits this shape:
11+
12+
```ignore (pseudo-rust)
13+
loop {
14+
state = 'blk: {
15+
match state {
16+
State::A => {
17+
break 'blk State::B
18+
}
19+
State::B => { /* ... */ }
20+
/* ... */
21+
}
22+
}
23+
}
24+
```
25+
26+
Here the loop itself can be annotated with `#[loop_match]`, and any `break 'blk` with
27+
`#[const_continue]` if the value is know at compile time:
28+
29+
```ignore (pseudo-rust)
30+
#[loop_match]
31+
loop {
32+
state = 'blk: {
33+
match state {
34+
State::A => {
35+
#[const_continue]
36+
break 'blk State::B
37+
}
38+
State::B => { /* ... */ }
39+
/* ... */
40+
}
41+
}
42+
}
43+
```
44+
45+
The observable behavior of this loop is exactly the same as without the extra attributes.
46+
The difference is in the generated output: normally, when the state is `A`, control flow
47+
moves from the `A` branch, back to the top of the loop, then to the `B` branch. With the
48+
attributes, The `A` branch will immediately jump to the `B` branch.
49+
50+
Removing the indirection can be beneficial for stack usage and branch prediction, and
51+
enables other optimizations by clearly splitting out the control flow paths that your
52+
program will actually use.

0 commit comments

Comments
 (0)