1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-11 21:22:20 +02:00

Merge pull request #141 from brandonpollack23/static_const

Added notes, runtime constant eval, and footnoes to static-and-const.md
This commit is contained in:
Martin Geisler 2023-01-10 18:53:46 +01:00 committed by GitHub
commit 1b3eaec5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,6 +24,8 @@ fn main() {
} }
``` ```
According the the [Rust RFC Book][1] these are inlined upon use.
## `static` ## `static`
You can also declare static variables: You can also declare static variables:
@ -36,4 +38,15 @@ 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 have a `'static` lifetime.
We will look at mutating static data in the [chapter on Unsafe Rust](../unsafe.md). We will look at mutating static data in the [chapter on Unsafe Rust](../unsafe.md).
<details>
* Mention that `const` behaves semantically similar to C++'s `constexpr`.
* `static`, on the other hand, is much more similar to a `const` or mutable global variable in C++.
* It isn't super common that one would need a runtime evaluated constant, but it is helpful and safer than using a static.
</details>