You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/patterns.md
+48-1Lines changed: 48 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -801,6 +801,9 @@ let Struct{a: x, b: y, c: z} = struct_value; // destructure all fields
801
801
r[patterns.struct.refutable]
802
802
A struct pattern is refutable if the [PathInExpression] resolves to a constructor of an enum with more than one variant, or one of its subpatterns is refutable.
803
803
804
+
r[patterns.struct.namespace]
805
+
A struct pattern matches against the struct, union, or enum variant whose constructor is resolved from [PathInExpression] in the [type namespace]. See [patterns.tuple-struct.namespace] for more details.
806
+
804
807
r[patterns.tuple-struct]
805
808
## Tuple struct patterns
806
809
@@ -818,6 +821,46 @@ They are also used to [destructure](#destructuring) a tuple struct or enum value
818
821
r[patterns.tuple-struct.refutable]
819
822
A tuple struct pattern is refutable if the [PathInExpression] resolves to a constructor of an enum with more than one variant, or one of its subpatterns is refutable.
820
823
824
+
r[patterns.tuple-struct.namespace]
825
+
A tuple struct pattern matches against the tuple struct or [tuple-like enum variant] whose constructor is resolved from [PathInExpression] in the [value namespace].
826
+
827
+
> [!NOTE]
828
+
> Conversely, a struct pattern for a tuple struct or [tuple-like enum variant], e.g. `S { 0: _ }`, matches against the tuple struct or variant whose constructor is resolved in the [type namespace].
829
+
>
830
+
> ```rust,no_run
831
+
> enum E1 { V(u16) }
832
+
> enum E2 { V(u32) }
833
+
>
834
+
> // Import `E1::V` from the type namespace only.
835
+
> mod _0 {
836
+
> const V: () = (); // For namespace masking.
837
+
> pub(super) use super::E1::*;
838
+
> }
839
+
> use _0::*;
840
+
>
841
+
> // Import `E2::V` from the value namespace only.
842
+
> mod _1 {
843
+
> struct V {} // For namespace masking.
844
+
> pub(super) use super::E2::*;
845
+
> }
846
+
> use _1::*;
847
+
>
848
+
> fn f() {
849
+
> // This struct pattern matches against the tuple-like
850
+
> // enum variant whose constructor was found in the type
851
+
> // namespace.
852
+
> let V { 0: ..=u16::MAX } = (loop {}) else { loop {} };
853
+
> // This tuple struct pattern matches against the tuple-like
854
+
> // enum variant whose constructor was found in the value
> # // Required due to the odd behavior of `super` within functions.
859
+
> # fn main() {}
860
+
> ```
861
+
>
862
+
> The Lang team has made certain decisions, such as in [PR #138458], that raise questions about the desirability of using the value namespace in this way for patterns, as described in [PR #140593]. It might be prudent to not intentionally rely on this nuance in your code.
863
+
821
864
r[patterns.tuple]
822
865
## Tuple patterns
823
866
@@ -1035,6 +1078,8 @@ This allows us to reserve syntactic space for a possible future type ascription
1035
1078
For example, `x @ A(..) | B(..)` will result in an error that `x` is not bound in all patterns.
1036
1079
`&A(x) | B(x)` will result in a type mismatch between `x` in the different subpatterns.
0 commit comments