|
1 | 1 | # Type system attributes
|
2 | 2 |
|
| 3 | +r[attributes.type-system] |
| 4 | + |
3 | 5 | The following [attributes] are used for changing how a type can be used.
|
4 | 6 |
|
5 | 7 | ## The `non_exhaustive` attribute
|
6 | 8 |
|
| 9 | +r[attributes.type-system.non_exhaustive] |
| 10 | + |
| 11 | + |
| 12 | +r[attributes.type-system.non_exhaustive.general] |
7 | 13 | The *`non_exhaustive` attribute* indicates that a type or variant may have
|
8 |
| -more fields or variants added in the future. It can be applied to |
9 |
| -[`struct`s][struct], [`enum`s][enum], and `enum` variants. |
| 14 | +more fields or variants added in the future. |
| 15 | + |
| 16 | +r[attributes.type-system.non_exhaustive.restriction] |
| 17 | +It can be applied to [`struct`s][struct], [`enum`s][enum], and `enum` variants. |
10 | 18 |
|
| 19 | +r[attributes.type-system.non_exhaustive.syntax] |
11 | 20 | The `non_exhaustive` attribute uses the [_MetaWord_] syntax and thus does not
|
12 | 21 | take any inputs.
|
13 | 22 |
|
| 23 | +r[attributes.type-system.non_exhaustive.same-crate] |
14 | 24 | Within the defining crate, `non_exhaustive` has no effect.
|
15 | 25 |
|
16 | 26 | ```rust
|
@@ -65,9 +75,11 @@ match message {
|
65 | 75 | }
|
66 | 76 | ```
|
67 | 77 |
|
| 78 | +r[attributes.type-system.non_exhaustive.external-crate] |
68 | 79 | Outside of the defining crate, types annotated with `non_exhaustive` have limitations that
|
69 | 80 | preserve backwards compatibility when new fields or variants are added.
|
70 | 81 |
|
| 82 | +r[attributes.type-system.non_exhaustive.construction] |
71 | 83 | Non-exhaustive types cannot be constructed outside of the defining crate:
|
72 | 84 |
|
73 | 85 | - Non-exhaustive variants ([`struct`][struct] or [`enum` variant][enum]) cannot be constructed
|
@@ -119,6 +131,7 @@ let message = Message::Reaction(0);
|
119 | 131 | let message = Message::Quit;
|
120 | 132 | ```
|
121 | 133 |
|
| 134 | +r[attributes.type-system.non_exhaustive.match] |
122 | 135 | There are limitations when matching on non-exhaustive types outside of the defining crate:
|
123 | 136 |
|
124 | 137 | - When pattern matching on a non-exhaustive variant ([`struct`][struct] or [`enum` variant][enum]),
|
|
0 commit comments