Skip to content

Commit 66921dc

Browse files
committed
combine the two together
1 parent 5ad21ca commit 66921dc

File tree

4 files changed

+84
-74
lines changed

4 files changed

+84
-74
lines changed

src/doc/trpl/SUMMARY.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
* [Closures](closures.md)
4444
* [Universal Function Call Syntax](ufcs.md)
4545
* [Crates and Modules](crates-and-modules.md)
46-
* [`static`](static.md)
47-
* [`const`](const.md)
46+
* [`const` and `static`](const-and-static.md)
4847
* [Tuple Structs](tuple-structs.md)
4948
* [Attributes](attributes.md)
5049
* [Conditional Compilation](conditional-compilation.md)

src/doc/trpl/const-and-static.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
% `const` and `static`
2+
3+
Rust has a way of defining constants with the `const` keyword:
4+
5+
```rust
6+
const N: i32 = 5;
7+
```
8+
9+
Unlike [`let`][let] bindings, you must annotate the type of a `const`.
10+
11+
[let]: variable-bindings.html
12+
13+
Constants live for the entire lifetime of a program. More specifically,
14+
constants in Rust have no fixed address in memory. This is because they’re
15+
effectively inlined to each place that they’re used. References to the same
16+
constant are not necessarily guaranteed to refer to the same memory address for
17+
this reason.
18+
19+
% `static`
20+
21+
Rust provides a ‘global variable’ sort of facility in static items. They’re
22+
similar to [constants][const], but static items aren’t inlined upon use. This
23+
means that there is only one instance for each value, and it’s at a fixed
24+
location in memory.
25+
26+
Here’s an example:
27+
28+
```rust
29+
static N: i32 = 5;
30+
```
31+
32+
[const]: const.html
33+
34+
Unlike [`let`][let] bindings, you must annotate the type of a `static`.
35+
36+
[let]: variable-bindings.html
37+
38+
Statics live for the entire lifetime of a program, and therefore any
39+
reference stored in a constant has a [`’static` lifetime][lifetimes]:
40+
41+
```rust
42+
static NAME: &'static str = "Steve";
43+
```
44+
45+
[lifetimes]: lifetimes.html
46+
47+
## Mutability
48+
49+
You can introduce mutability with the `mut` keyword:
50+
51+
```rust
52+
static mut N: i32 = 5;
53+
```
54+
55+
Because this is mutable, one thread could be updating `N` while another is
56+
reading it, causing memory unsafety. As such both accessing and mutating a
57+
`static mut` is [`unsafe`][unsafe], and so must be done in an `unsafe` block:
58+
59+
```rust
60+
# static mut N: i32 = 5;
61+
62+
unsafe {
63+
N += 1;
64+
65+
println!("N: {}", N);
66+
}
67+
```
68+
69+
Furthermore, any type stored in a `static` must be `Sync`.
70+
71+
# Initializing
72+
73+
Both `const` and `static` have requirements for giving them a value. They may
74+
only be given a value that’s a constant expression. In other words, you cannot
75+
use the result of a function call or anything similarly complex or at runtime.
76+
77+
# Which construct should I use?
78+
79+
Almost always, if you can choose between the two, choose `const`. Global state
80+
is difficult to manage, and `const` has better, easier-to-follow restrictions.
81+
82+
`static mut` can be useful when wrapping a C library that needs some sort of
83+
global state to operate.

src/doc/trpl/const.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/doc/trpl/static.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)