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
Syntactically, the inferred const is an expression. Semantically,
though, it's a new kind of const generic argument. Let's add the
necessary language in various places to make this all clear, and let's
fix the grammar for `GenericArgsConst` correspondingly.
Copy file name to clipboardExpand all lines: src/expressions/array-expr.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,7 @@ r[expr.array.length-operand]
35
35
The expression after the `;` is called the *length operand*.
36
36
37
37
r[expr.array.length-restriction]
38
-
It must have type `usize` and be a [constant expression], such as a [literal] or a [constant item].
38
+
The length operand must either be an [inferred const] or be a [constant expression] of type `usize` (e.g. a [literal] or a [constant item]).
39
39
40
40
r[expr.array.repeat-behavior]
41
41
An array expression of this form creates an array with the length of the value of the length operand with each element being a copy of the repeat operand.
@@ -113,6 +113,7 @@ The array index expression can be implemented for types other than arrays and sl
Copy file name to clipboardExpand all lines: src/items/generics.md
+11-4Lines changed: 11 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -172,17 +172,24 @@ fn example() {
172
172
```
173
173
174
174
r[items.generics.const.inferred]
175
-
Where a const argument is expected, an `_` (optionally surrounding by any number of matching parentheses), called the "inferred const", can be used instead. This asks the compiler to infer the const argument if possible based on surrounding information.
175
+
Where a const argument is expected, an `_` (optionally surrounding by any number of matching parentheses), called the *inferred const* ([grammar][InferredConst], [path rules][paths.expr.complex-const-params]), can be used instead. This asks the compiler to infer the const argument if possible based on surrounding information.
176
176
177
177
```rust
178
-
fnmake_buf() -> [u8; 1024] {
178
+
fnmake_buf<constN:usize>() -> [u8; N] {
179
179
[0x1; _]
180
-
// ^ Infers `1024`.
180
+
// ^ Infers `N`.
181
181
}
182
+
let_: [u8; 1024] =make_buf::<_>();
183
+
// ^ Infers `1024`.
182
184
```
183
185
184
186
r[items.generics.const.inferred.constraint]
185
-
It cannot be used in item signatures.
187
+
The inferred const cannot be used in item signatures.
188
+
189
+
```rust,compile_fail
190
+
fn f<const N: usize>(x: [u8; N]) -> [u8; _] { x }
191
+
// ^ ERROR
192
+
```
186
193
187
194
r[items.generics.const.type-ambiguity]
188
195
When there is ambiguity if a generic argument could be resolved as either a
0 commit comments