mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-26 17:23:01 +02:00
Rephrase static-and-const (#601)
* Rephrase static-and-const * Reduce size, add table to compare static vs const
This commit is contained in:
parent
bc972711a2
commit
e99df4ae5a
@ -1,10 +1,12 @@
|
||||
# Static and Constant Variables
|
||||
|
||||
Globally-scoped names for values can be given with static variables and constant definitions.
|
||||
Static and constant variables are two different ways to create globally-scoped values that
|
||||
cannot be moved or reallocated during the execution of the program.
|
||||
|
||||
## `const`
|
||||
|
||||
You can declare compile-time constants:
|
||||
Constant variables are evaluated at compile time and their values are inlined
|
||||
wherever they are used:
|
||||
|
||||
```rust,editable
|
||||
const DIGEST_SIZE: usize = 3;
|
||||
@ -26,9 +28,11 @@ fn main() {
|
||||
|
||||
According to the [Rust RFC Book][1] these are inlined upon use.
|
||||
|
||||
Only functions marked `const` can be called at compile time to generate `const` values. `const` functions can however be called at runtime.
|
||||
|
||||
## `static`
|
||||
|
||||
You can also declare static variables:
|
||||
Static variables will live during the whole execution of the program, and therefore will not move:
|
||||
|
||||
```rust,editable
|
||||
static BANNER: &str = "Welcome to RustOS 3.14";
|
||||
@ -38,7 +42,8 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
As noted in the [Rust RFC Book][1], these are not inlined upon use and have an actual associated memory location. This is useful for unsafe and embedded code, and the variable lives through the entirety of the program execution.
|
||||
As noted in the [Rust RFC Book][1], these are not inlined upon use and have an actual associated memory location. This is useful for unsafe and
|
||||
embedded code, and the variable lives through the entirety of the program execution.
|
||||
When a globally-scoped value does not have a reason to need object identity, `const` is generally preferred.
|
||||
|
||||
Because `static` variables are accessible from any thread, they must be `Sync`. Interior mutability
|
||||
@ -53,6 +58,18 @@ mutable statics](../unsafe/mutable-static-variables.md) in the chapter on Unsafe
|
||||
* `static`, on the other hand, is much more similar to a `const` or mutable global variable in C++.
|
||||
* `static` provides object identity: an address in memory and state as required by types with interior mutability such as `Mutex<T>`.
|
||||
* It isn't super common that one would need a runtime evaluated constant, but it is helpful and safer than using a static.
|
||||
* `thread_local` data can be created with the macro `std::thread_local`.
|
||||
|
||||
### Properties table:
|
||||
|
||||
| Property | Static | Constant |
|
||||
|---|---|---|
|
||||
| Has an address in memory | Yes | No (inlined) |
|
||||
| Lives for the entire duration of the program | Yes | No |
|
||||
| Can be mutable | Yes (unsafe) | No |
|
||||
| Evaluated at compile time | Yes (initialised at compile time) | Yes |
|
||||
| Inlined wherever it is used | No | Yes |
|
||||
|
||||
|
||||
</details>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user