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