|
14 | 14 | Array expressions come in two forms.
|
15 | 15 |
|
16 | 16 | The first form lists out every value in the array.
|
17 |
| -The syntax for this form is a comma-separated list of operands of uniform type enclosed in square brackets. |
| 17 | +The syntax for this form is a comma-separated list of expressions of uniform type enclosed in square brackets. |
18 | 18 | This produces an array containing each of these values in the order they are written.
|
19 | 19 |
|
20 |
| -The syntax for the second form is two operands separated by a semicolon (`;`) enclosed in square brackets. |
21 |
| -The operand after the `;` must have type `usize` and be a [constant expression], such as a [literal] or a [constant item]. |
22 |
| -`[a; b]` creates an array containing `b` copies of the value of `a`. |
23 |
| -If the operand after the semicolon has a value greater than 1 then this requires that the type of `a` is [`Copy`], or `a` must be a path to a constant item. |
| 20 | +The syntax for the second form is two expressions separated by a semicolon (`;`) enclosed in square brackets. |
| 21 | +The expression before the `;` is called the *repeat operand*. |
| 22 | +The expression after the `;` is called the *length operand*. |
| 23 | +It must have type `usize` and be a [constant expression], such as a [literal] or a [constant item]. |
| 24 | +An array expression of this form creates an array with the length of the value of the legnth operand with each element a copy of the repeat operand. |
| 25 | +That is, `[a; b]` creates an array containing `b` copies of the value of `a`. |
| 26 | +If the length operand has a value greater than 1 then this requires that the type of the repeat operand is [`Copy`] or that it must be a [path] to a constant item. |
24 | 27 |
|
25 |
| -When the repeat expression, `a`, is a constant item, it is evaluated `b` times. |
26 |
| -If `b` is 0, the constant item is not evaluated at all. |
27 |
| -For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied `b` times. |
| 28 | +When the repeat operand is a constant item, it is evaluated the length operand's value times. |
| 29 | +If that value is `0`, then the constant item is not evaluated at all. |
| 30 | +For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied the length operand's value times. |
28 | 31 |
|
29 | 32 | <div class="warning">
|
30 | 33 |
|
31 |
| -Warning: In the case where `b` is 0, and `a` is a non-constant item, there is currently a bug in `rustc` where the value `a` is evaluated but not dropped, thus causing a leak. |
| 34 | +Warning: In the case where the length operand is 0, and the repeat operand is a non-constant item, there is currently a bug in `rustc` where the value `a` is evaluated but not dropped, thus causing a leak. |
32 | 35 | See [issue #74836](https://github.com/rust-lang/rust/issues/74836).
|
33 | 36 |
|
34 | 37 | </div>
|
@@ -95,4 +98,5 @@ The array index expression can be implemented for types other than arrays and sl
|
95 | 98 | [constant item]: ../items/constant-items.md
|
96 | 99 | [literal]: ../tokens.md#literals
|
97 | 100 | [memory location]: ../expressions.md#place-expressions-and-value-expressions
|
| 101 | +[path]: path-expr.md |
98 | 102 | [slice]: ../types/slice.md
|
0 commit comments