Skip to content

Commit

Permalink
Document [a; 0] behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Dec 27, 2020
1 parent 36edce3 commit 3d5a774
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/expressions/array-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 3d5a774

Please sign in to comment.