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
This removes the grammar rules StructExprTuple and StructExprUnit, and
removes these as distinct expressions. Instead, a note block is used to
let the reader know how the constructors can be accessed in the value
namespace.
This stems back to the beginning of this documentation, which presumably
was presenting these as distinct kinds of expressions just as a
simplification or to match what people's mental models might be.
However, they are not distinct expressions, and I think it is misleading
to pretend that they are.
Closesrust-lang#1802
> Tuple structs and tuple enum variants are typically instantiated using a [call expression][expr.call] referring to the [constructor in the value namespace][items.struct.tuple]. These are distinct from a struct expression using curly braces referring to the constructor in the type namespace.
41
+
>
42
+
> ```rust
43
+
> structPosition(i32, i32, i32);
44
+
> Position(0, 0, 0); // Typical way of creating a tuple struct.
45
+
> letc=Position; // `c` is a function that takes 3 arguments.
46
+
> letpos=c(8, 6, 7); // Creates a `Position` value.
> // Gamma unit value, referring to the const in the value namespace.
74
+
> leta=Gamma;
75
+
> // Exact same value as `a`, but constructed using a struct expression
76
+
> // referring to the type namespace.
77
+
> letb=Gamma {};
78
+
>
79
+
> enumColorSpace { Oklch }
80
+
> letc=ColorSpace::Oklch;
81
+
> letd=ColorSpace::Oklch {};
82
+
> ```
83
+
54
84
r[expr.struct.field]
55
85
## Fieldstructexpression
56
86
@@ -85,7 +115,7 @@ drop(y_ref);
85
115
```
86
116
87
117
r[expr.struct.brace-restricted-positions]
88
-
Struct expressions with curly braces can't be used directly in a [loop] or [if] expression's head, or in the [scrutinee] of an [if let] or [match] expression.
118
+
Struct expressions can't be used directly in a [loop] or [if] expression's head, or in the [scrutinee] of an [if let] or [match] expression.
89
119
However, struct expressions can be used in these situations if they are within another expression, for example inside [parentheses].
A struct expression with fields enclosed in parentheses constructs a tuple struct or a tuple variant of an enum.
122
-
Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's (enum tuple variant's) constructor. For example:
123
-
124
-
```rust
125
-
structPosition(i32, i32, i32);
126
-
Position(0, 0, 0); // Typical way of creating a tuple struct.
127
-
letc=Position; // `c` is a function that takes 3 arguments.
128
-
letpos=c(8, 6, 7); // Creates a `Position` value.
129
-
130
-
enumVersion { Triple(i32, i32, i32) };
131
-
Version::Triple(0, 0, 0);
132
-
letf=Version::Triple;
133
-
letver=f(8, 6, 7);
134
-
```
135
-
136
-
> [!NOTE]
137
-
> While the grammar permits qualified paths, the last segment can't be a type alias:
138
-
>
139
-
> ```rust
140
-
> traitTr { typeT; }
141
-
> impl<T> TrforT { typeT=T; }
142
-
>
143
-
> structTuple();
144
-
> enumEnum { Tuple() }
145
-
>
146
-
> // <Unit as Tr>::T(); // causes an error -- `::T` is a type, not a value
0 commit comments