From 3d5a77402ec00f2f50419d4b72dffeb8337094dd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 27 Dec 2020 11:51:46 -0800 Subject: [PATCH] Document [a; 0] behavior. --- src/expressions/array-expr.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 5a7079942..d759e62ab 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -16,14 +16,21 @@ 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 +separated by a semicolon. 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 +copies of the value of `a`. If the expression `b` after the semicolon has a value greater than 1 then this requires that the type of `a` is [`Copy`](../special-types-and-traits.md#copy), or `a` must be a path to a -constant item. +constant item. When `b` evaluates to 0, the expression `a` is evaluated once +unless it is a path to a constant item, in which case the constant is not +instantiated. + +> **Note:** In the case where `b` is 0, and `a` is a non-const expression, +> 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];