Skip to content

Document underscore_const_names #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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