Skip to content

Commit

Permalink
Merge pull request rust-lang#620 from rust-lang-nursery/docs-undersco…
Browse files Browse the repository at this point in the history
…re_const_names

Document `underscore_const_names`
  • Loading branch information
ehuss authored Jun 17, 2019
2 parents 95cb608 + d4f7ad4 commit 08ae27a
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/items/constant-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

> **<sup>Syntax</sup>**\
> _ConstantItem_ :\
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_] `=` [_Expression_] `;`
> &nbsp;&nbsp; `const` ( [IDENTIFIER] | `_` ) `:` [_Type_] `=` [_Expression_] `;`
A *constant item* is a named _[constant value]_ which is not associated with a
specific memory location in the program. Constants are essentially inlined
A *constant item* is an optionally named _[constant value]_ which is not associated
with a specific memory location in the program. Constants are essentially inlined
wherever they are used, meaning that they are copied directly into the relevant
context when used. References to the same constant are not necessarily
guaranteed to refer to the same memory address.
Expand Down Expand Up @@ -60,8 +60,37 @@ fn create_and_drop_zero_with_destructor() {
}
```

## Unnamed constant

Unlike an [associated] constant, a [free] constant may be unnamed by using
an underscore instead of the name. For example:

```rust
const _: () = { struct _SameNameTwice; };

// OK although it is the same name as above:
const _: () = { struct _SameNameTwice; };
```

As with [underscore imports], macros may safely emit the same unnamed constant in
the same scope more than once. For example, the following should not produce an error:

```rust
macro_rules! m {
($item: item) => { $item $item }
}

m!(const _: () = (););
// This expands to:
// const _: () = ();
// const _: () = ();
```

[associated]: glossary.html#associated-item
[constant value]: const_eval.html#constant-expressions
[free]: glossary.html#free-item
[static lifetime elision]: lifetime-elision.html#static-lifetime-elision
[IDENTIFIER]: identifiers.html
[underscore imports]: items/use-declarations.html#underscore-imports
[_Type_]: types.html#type-expressions
[_Expression_]: expressions.html

0 comments on commit 08ae27a

Please sign in to comment.