1010>   ;  ;   ;  ; [ _ Expression_ ] ( ` , ` [ _ Expression_ ] )<sup >\* </sup > ` , ` <sup >?</sup >\
1111>   ;  ; | [ _ Expression_ ] ` ; ` [ _ Expression_ ]
1212
13- An _ [ array] ( ../types/array.md ) expression_ can be written by
14- enclosing zero or more comma-separated expressions of uniform type in square
15- brackets. This produces an array containing each of these values in the
16- order they are written.
13+ An _ [ array] expression_ can be written by enclosing zero or more
14+ comma-separated expressions of uniform type in square brackets. This produces
15+ an array containing each of these values in the order they are written.
1716
1817Alternatively there can be exactly two expressions inside the brackets,
19- separated by a semi-colon. The expression after the ` ; ` must have type
20- ` usize ` and be a [ constant expression] ,
21- such as a [ literal] ( ../tokens.md#literals ) or a [ constant
22- item] ( ../items/constant-items.md ) . ` [a; b] ` creates an array containing ` b `
23- copies of the value of ` a ` . If the expression after the semi-colon has a value
24- greater than 1 then this requires that the type of ` a ` is
25- [ ` Copy ` ] ( ../special-types-and-traits.md#copy ) .
18+ separated by a semicolon. The expression after the ` ; ` must have type ` usize `
19+ and be a [ constant expression] , such as a [ literal] or a [ constant item] . `[ a;
20+ b] ` creates an array containing ` b` copies of the value of ` a`. If the
21+ expression after the semicolon has a value greater than 1 then this requires
22+ that the type of ` a ` is [ ` Copy ` ] , or ` a ` must be a path to a constant item.
23+
24+ When the repeat expression ` a ` is a constant item, it is evaluated ` b ` times.
25+ If ` b ` is 0, the constant item is not evaluated at all. For expressions that
26+ are not a constant item, it is evaluated exactly once, and then the result is
27+ copied ` b ` times.
28+
29+ <div class =" warning " >
30+
31+ Warning: In the case where ` b ` is 0, and ` a ` is a non-constant item, there is
32+ currently a bug in ` rustc ` where the value ` a ` is evaluated but not dropped,
33+ thus causing a leak. See [ issue
34+ #74836 ] ( https://github.com/rust-lang/rust/issues/74836 ) .
35+
36+ </div >
2637
2738``` rust
2839[1 , 2 , 3 , 4 ];
2940[" a" , " b" , " c" , " d" ];
3041[0 ; 128 ]; // array with 128 zeros
3142[0u8 , 0u8 , 0u8 , 0u8 ,];
3243[[1 , 0 , 0 ], [0 , 1 , 0 ], [0 , 0 , 1 ]]; // 2D array
44+ const EMPTY : Vec <i32 > = Vec :: new ();
45+ [EMPTY ; 2 ];
3346```
3447
3548### Array expression attributes
@@ -44,10 +57,9 @@ expressions].
4457> _ IndexExpression_ :\
4558>   ;  ; [ _ Expression_ ] ` [ ` [ _ Expression_ ] ` ] `
4659
47- [ Array] ( ../types/array.md ) and [ slice] ( ../types/slice.md ) -typed expressions can be
48- indexed by writing a square-bracket-enclosed expression of type ` usize ` (the
49- index) after them. When the array is mutable, the resulting [ memory location]
50- can be assigned to.
60+ [ Array] and [ slice] -typed expressions can be indexed by writing a
61+ square-bracket-enclosed expression of type ` usize ` (the index) after them.
62+ When the array is mutable, the resulting [ memory location] can be assigned to.
5163
5264For other types an index expression ` a[b] ` is equivalent to
5365` *std::ops::Index::index(&a, b) ` , or
@@ -81,11 +93,16 @@ arr[10]; // warning: index out of bounds
8193The array index expression can be implemented for types other than arrays and slices
8294by implementing the [ Index] and [ IndexMut] traits.
8395
96+ [ `Copy` ] : ../special-types-and-traits.md#copy
8497[ IndexMut ] : ../../std/ops/trait.IndexMut.html
8598[ Index ] : ../../std/ops/trait.Index.html
8699[ Inner attributes ] : ../attributes.md
87100[ _Expression_ ] : ../expressions.md
88101[ _InnerAttribute_ ] : ../attributes.md
102+ [ array ] : ../types/array.md
89103[ attributes on block expressions ] : block-expr.md#attributes-on-block-expressions
90104[ constant expression ] : ../const_eval.md#constant-expressions
105+ [ constant item ] : ../items/constant-items.md
106+ [ literal ] : ../tokens.md#literals
91107[ memory location ] : ../expressions.md#place-expressions-and-value-expressions
108+ [ slice ] : ../types/slice.md
0 commit comments