From 8027bc8ca966c20c53b8e152cf8cf5456d5f06e3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 28 Dec 2020 08:13:20 -0800 Subject: [PATCH 1/4] Move links to bottom of the file. --- src/expressions/array-expr.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 1b0699890..098cbec1b 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -10,19 +10,16 @@ >       [_Expression_] ( `,` [_Expression_] )\* `,`?\ >    | [_Expression_] `;` [_Expression_] -An _[array](../types/array.md) expression_ can be written by -enclosing zero or more comma-separated expressions of uniform type in square -brackets. This produces an array containing each of these values in the -order they are written. +An _[array] expression_ can be written by enclosing zero or more +comma-separated expressions of uniform type in square brackets. This produces +an array containing each of these values in the order they are written. Alternatively there can be exactly two expressions inside the brackets, -separated by a semi-colon. The expression after the `;` must have type -`usize` and be a [constant expression], -such as a [literal](../tokens.md#literals) or a [constant -item](../items/constant-items.md). `[a; b]` creates an array containing `b` -copies of the value of `a`. If the expression after the semi-colon has a value -greater than 1 then this requires that the type of `a` is -[`Copy`](../special-types-and-traits.md#copy). +separated by a semicolon. The expression after the `;` must have type `usize` +and be a [constant expression], such as a [literal] or a [constant item]. `[a; +b]` creates an array containing `b` copies of the value of `a`. If the +expression after the semicolon has a value greater than 1 then this requires +that the type of `a` is [`Copy`]. ```rust [1, 2, 3, 4]; @@ -44,10 +41,9 @@ expressions]. > _IndexExpression_ :\ >    [_Expression_] `[` [_Expression_] `]` -[Array](../types/array.md) and [slice](../types/slice.md)-typed expressions can be -indexed by writing a square-bracket-enclosed expression of type `usize` (the -index) after them. When the array is mutable, the resulting [memory location] -can be assigned to. +[Array] and [slice]-typed expressions can be indexed by writing a +square-bracket-enclosed expression of type `usize` (the index) after them. +When the array is mutable, the resulting [memory location] can be assigned to. For other types an index expression `a[b]` is equivalent to `*std::ops::Index::index(&a, b)`, or @@ -81,11 +77,16 @@ arr[10]; // warning: index out of bounds The array index expression can be implemented for types other than arrays and slices by implementing the [Index] and [IndexMut] traits. +[`Copy`]: ../special-types-and-traits.md#copy [IndexMut]: ../../std/ops/trait.IndexMut.html [Index]: ../../std/ops/trait.Index.html [Inner attributes]: ../attributes.md [_Expression_]: ../expressions.md [_InnerAttribute_]: ../attributes.md +[array]: ../types/array.md [attributes on block expressions]: block-expr.md#attributes-on-block-expressions [constant expression]: ../const_eval.md#constant-expressions +[constant item]: ../items/constant-items.md +[literal]: ../tokens.md#literals [memory location]: ../expressions.md#place-expressions-and-value-expressions +[slice]: ../types/slice.md From e031b14b3e98c7f0f066e7fd044a353d6d641227 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 22 Dec 2020 05:44:26 -0800 Subject: [PATCH 2/4] Document array expression with a const. --- src/expressions/array-expr.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 098cbec1b..8a98f1ee5 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -19,7 +19,7 @@ separated by a semicolon. The expression after the `;` must have type `usize` and be a [constant expression], such as a [literal] or a [constant item]. `[a; b]` creates an array containing `b` copies of the value of `a`. If the expression after the semicolon has a value greater than 1 then this requires -that the type of `a` is [`Copy`]. +that the type of `a` is [`Copy`], or `a` must be a path to a constant item. ```rust [1, 2, 3, 4]; @@ -27,6 +27,8 @@ that the type of `a` is [`Copy`]. [0; 128]; // array with 128 zeros [0u8, 0u8, 0u8, 0u8,]; [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D array +const EMPTY: Vec = Vec::new(); +[EMPTY; 2]; ``` ### Array expression attributes From a2e5a140fae5b95af901d14936b3e3ec818ecd12 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 28 Dec 2020 08:23:09 -0800 Subject: [PATCH 3/4] Document [a; 0] behavior. --- src/expressions/array-expr.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 8a98f1ee5..a23e6d781 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -21,6 +21,16 @@ b]` creates an array containing `b` copies of the value of `a`. If the expression 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. +When the repeat expression `a` is a constant item, it is evaluated `b` times. +If `b` is 0, the constant item is not evaluated at all. For expressions that +are not a constant item, it is evaluated exactly once, and then the result is +copied `b` times. + +> **Note:** 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. See [issue +> #74836](https://github.com/rust-lang/rust/issues/74836). + ```rust [1, 2, 3, 4]; ["a", "b", "c", "d"]; From 0feff1c68bbedc2d5c93d5464a50f8d94998322a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 29 Dec 2020 08:20:15 -0800 Subject: [PATCH 4/4] Switch note to warning. --- src/expressions/array-expr.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index a23e6d781..0fd5427a9 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -26,10 +26,14 @@ If `b` is 0, the constant item is not evaluated at all. For expressions that are not a constant item, it is evaluated exactly once, and then the result is copied `b` times. -> **Note:** 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. See [issue -> #74836](https://github.com/rust-lang/rust/issues/74836). +
+ +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. See [issue +#74836](https://github.com/rust-lang/rust/issues/74836). + +
```rust [1, 2, 3, 4];