mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-19 04:48:12 +02:00
Clarify a couple minor points (#912)
* compound types: disambiguate that length is not 'same' * basic syntax: clarify preference for consts and quickly justify unsafety of static mut * basic-syntax: take review feedback into account on consts/statics * basic-syntax: be careful not to call `const` defns variables variables in rust are `place`s in a formal sense and name objects in an informal one; `const` merely abbreviates an expression
This commit is contained in:
parent
6d736d69c4
commit
abb05164da
@ -31,7 +31,7 @@ Key points:
|
||||
|
||||
Arrays:
|
||||
|
||||
* Arrays have elements of the same type, `T`, and length, `N`, which is a compile-time constant.
|
||||
* A value of the array type `[T; N]` holds `N` (a compile-time constant) elements of the same type `T`.
|
||||
Note that the length of the array is *part of its type*, which means that `[u8; 3]` and
|
||||
`[u8; 4]` are considered two different types.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Static and Constant Variables
|
||||
|
||||
Global state is managed with static and constant variables.
|
||||
Globally-scoped names for values can be given with static variables and constant definitions.
|
||||
|
||||
## `const`
|
||||
|
||||
@ -39,14 +39,17 @@ 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.
|
||||
When a globally-scoped value does not have a reason to need object identity, `const` is generally preferred.
|
||||
|
||||
|
||||
We will look at mutating static data in the [chapter on Unsafe Rust](../unsafe.md).
|
||||
We will look at [mutating static data](../unsafe/mutable-static-variables.md) in the chapter on Unsafe Rust.
|
||||
Because `static` variables are accessible from any thread, mutable static variables require manual, unsafe, synchronization of accesses.
|
||||
|
||||
<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++.
|
||||
* `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.
|
||||
|
||||
</details>
|
||||
|
Loading…
x
Reference in New Issue
Block a user