]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/items/constant-items.md
New upstream version 1.51.0+dfsg1
[rustc.git] / src / doc / reference / src / items / constant-items.md
CommitLineData
ea8adc8c
XL
1# Constant items
2
0bf4aa26
XL
3> **<sup>Syntax</sup>**\
4> _ConstantItem_ :\
5869c6ff 5> &nbsp;&nbsp; `const` ( [IDENTIFIER] | `_` ) `:` [_Type_] ( `=` [_Expression_] )<sup>?</sup> `;`
ff7c6d11 6
dc9dc135
XL
7A *constant item* is an optionally named _[constant value]_ which is not associated
8with a specific memory location in the program. Constants are essentially inlined
ea8adc8c 9wherever they are used, meaning that they are copied directly into the relevant
3dfed10e
XL
10context when used. This includes usage of constants from external crates, and
11non-[`Copy`] types. References to the same constant are not necessarily
ea8adc8c
XL
12guaranteed to refer to the same memory address.
13
ff7c6d11 14Constants must be explicitly typed. The type must have a `'static` lifetime: any
3dfed10e 15references in the initializer must have `'static` lifetimes.
ff7c6d11
XL
16
17Constants may refer to the address of other constants, in which case the
ea8adc8c 18address will have elided lifetimes where applicable, otherwise – in most cases
ff7c6d11 19– defaulting to the `static` lifetime. (See [static lifetime
ea8adc8c
XL
20elision].) The compiler is, however, still at liberty to translate the constant
21many times, so the address referred to may not be stable.
22
ea8adc8c
XL
23```rust
24const BIT1: u32 = 1 << 0;
25const BIT2: u32 = 1 << 1;
26
27const BITS: [u32; 2] = [BIT1, BIT2];
28const STRING: &'static str = "bitstring";
29
30struct BitsNStrings<'a> {
31 mybits: [u32; 2],
32 mystring: &'a str,
33}
34
35const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
36 mybits: BITS,
37 mystring: STRING,
38};
39```
abe05a73 40
5869c6ff
XL
41The constant expression may only be omitted in a [trait definition].
42
ff7c6d11
XL
43## Constants with Destructors
44
8faf50e0 45Constants can contain destructors. Destructors are run when the value goes out
ff7c6d11
XL
46of scope.
47
48```rust
49struct TypeWithDestructor(i32);
50
51impl Drop for TypeWithDestructor {
52 fn drop(&mut self) {
53 println!("Dropped. Held {}.", self.0);
54 }
55}
56
57const ZERO_WITH_DESTRUCTOR: TypeWithDestructor = TypeWithDestructor(0);
58
59fn create_and_drop_zero_with_destructor() {
60 let x = ZERO_WITH_DESTRUCTOR;
61 // x gets dropped at end of function, calling drop.
62 // prints "Dropped. Held 0.".
63}
64```
65
dc9dc135
XL
66## Unnamed constant
67
68Unlike an [associated] constant, a [free] constant may be unnamed by using
69an underscore instead of the name. For example:
70
71```rust
72const _: () = { struct _SameNameTwice; };
73
74// OK although it is the same name as above:
75const _: () = { struct _SameNameTwice; };
76```
77
78As with [underscore imports], macros may safely emit the same unnamed constant in
79the same scope more than once. For example, the following should not produce an error:
80
81```rust
82macro_rules! m {
83 ($item: item) => { $item $item }
84}
85
86m!(const _: () = (););
87// This expands to:
88// const _: () = ();
89// const _: () = ();
90```
91
416331ca
XL
92[associated]: ../glossary.md#associated-item
93[constant value]: ../const_eval.md#constant-expressions
94[free]: ../glossary.md#free-item
95[static lifetime elision]: ../lifetime-elision.md#static-lifetime-elision
5869c6ff 96[trait definition]: traits.md
416331ca
XL
97[IDENTIFIER]: ../identifiers.md
98[underscore imports]: use-declarations.md#underscore-imports
99[_Type_]: ../types.md#type-expressions
100[_Expression_]: ../expressions.md
3dfed10e 101[`Copy`]: ../special-types-and-traits.md#copy