10
10
>   ;  ;   ;  ; [ _ Expression_ ] ( ` , ` [ _ Expression_ ] )<sup >\* </sup > ` , ` <sup >?</sup >\
11
11
>   ;  ; | [ _ Expression_ ] ` ; ` [ _ Expression_ ]
12
12
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.
17
16
18
17
Alternatively 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 >
26
37
27
38
``` rust
28
39
[1 , 2 , 3 , 4 ];
29
40
[" a" , " b" , " c" , " d" ];
30
41
[0 ; 128 ]; // array with 128 zeros
31
42
[0u8 , 0u8 , 0u8 , 0u8 ,];
32
43
[[1 , 0 , 0 ], [0 , 1 , 0 ], [0 , 0 , 1 ]]; // 2D array
44
+ const EMPTY : Vec <i32 > = Vec :: new ();
45
+ [EMPTY ; 2 ];
33
46
```
34
47
35
48
### Array expression attributes
@@ -44,10 +57,9 @@ expressions].
44
57
> _ IndexExpression_ :\
45
58
>   ;  ; [ _ Expression_ ] ` [ ` [ _ Expression_ ] ` ] `
46
59
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.
51
63
52
64
For other types an index expression ` a[b] ` is equivalent to
53
65
` *std::ops::Index::index(&a, b) ` , or
@@ -81,11 +93,16 @@ arr[10]; // warning: index out of bounds
81
93
The array index expression can be implemented for types other than arrays and slices
82
94
by implementing the [ Index] and [ IndexMut] traits.
83
95
96
+ [ `Copy` ] : ../special-types-and-traits.md#copy
84
97
[ IndexMut ] : ../../std/ops/trait.IndexMut.html
85
98
[ Index ] : ../../std/ops/trait.Index.html
86
99
[ Inner attributes ] : ../attributes.md
87
100
[ _Expression_ ] : ../expressions.md
88
101
[ _InnerAttribute_ ] : ../attributes.md
102
+ [ array ] : ../types/array.md
89
103
[ attributes on block expressions ] : block-expr.md#attributes-on-block-expressions
90
104
[ constant expression ] : ../const_eval.md#constant-expressions
105
+ [ constant item ] : ../items/constant-items.md
106
+ [ literal ] : ../tokens.md#literals
91
107
[ memory location ] : ../expressions.md#place-expressions-and-value-expressions
108
+ [ slice ] : ../types/slice.md
0 commit comments