]> git.proxmox.com Git - rustc.git/blame - src/doc/trpl/const-and-static.md
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / doc / trpl / const-and-static.md
CommitLineData
bd371182
AL
1% `const` and `static`
2
3Rust has a way of defining constants with the `const` keyword:
4
5```rust
6const N: i32 = 5;
7```
8
9Unlike [`let`][let] bindings, you must annotate the type of a `const`.
10
11[let]: variable-bindings.html
12
13Constants live for the entire lifetime of a program. More specifically,
14constants in Rust have no fixed address in memory. This is because they’re
15effectively inlined to each place that they’re used. References to the same
16constant are not necessarily guaranteed to refer to the same memory address for
17this reason.
18
19# `static`
20
21Rust provides a ‘global variable’ sort of facility in static items. They’re
22similar to constants, but static items aren’t inlined upon use. This means that
23there is only one instance for each value, and it’s at a fixed location in
24memory.
25
26Here’s an example:
27
28```rust
29static N: i32 = 5;
30```
31
32Unlike [`let`][let] bindings, you must annotate the type of a `static`.
33
bd371182 34Statics live for the entire lifetime of a program, and therefore any
62682a34 35reference stored in a constant has a [`'static` lifetime][lifetimes]:
bd371182
AL
36
37```rust
38static NAME: &'static str = "Steve";
39```
40
41[lifetimes]: lifetimes.html
42
43## Mutability
44
45You can introduce mutability with the `mut` keyword:
46
47```rust
48static mut N: i32 = 5;
49```
50
51Because this is mutable, one thread could be updating `N` while another is
52reading it, causing memory unsafety. As such both accessing and mutating a
53`static mut` is [`unsafe`][unsafe], and so must be done in an `unsafe` block:
54
55```rust
56# static mut N: i32 = 5;
57
58unsafe {
59 N += 1;
60
61 println!("N: {}", N);
62}
63```
64
65[unsafe]: unsafe.html
66
62682a34
SL
67Furthermore, any type stored in a `static` must be `Sync`, and may not have
68a [`Drop`][drop] implementation.
69
70[drop]: drop.html
bd371182
AL
71
72# Initializing
73
74Both `const` and `static` have requirements for giving them a value. They may
75only be given a value that’s a constant expression. In other words, you cannot
76use the result of a function call or anything similarly complex or at runtime.
77
78# Which construct should I use?
79
80Almost always, if you can choose between the two, choose `const`. It’s pretty
81rare that you actually want a memory location associated with your constant,
82and using a const allows for optimizations like constant propagation not only
83in your crate but downstream crates.