-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated static and const #1881
Updated static and const #1881
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -62,7 +62,9 @@ | |||||||||||
- [Named Structs](user-defined-types/named-structs.md) | ||||||||||||
- [Tuple Structs](user-defined-types/tuple-structs.md) | ||||||||||||
- [Enums](user-defined-types/enums.md) | ||||||||||||
- [Static and Const](user-defined-types/static-and-const.md) | ||||||||||||
- [Static and Const](user-defined-types/static.md) | ||||||||||||
- [Const](user-defined-types/const.md) | ||||||||||||
- [Properties](user-defined-types/properties.md) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Per my suggestion, could you remove the properties slide and then remove the extra level of nesting here? The title of the first slide can also change to just "Statics" since it's not introducing both concepts anymore. |
||||||||||||
- [Type Aliases](user-defined-types/aliases.md) | ||||||||||||
- [Exercise: Elevator Events](user-defined-types/exercise.md) | ||||||||||||
- [Solution](user-defined-types/solution.md) | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||
# `const` | ||||||
|
||||||
Constant variables are evaluated at compile time and their values are inlined | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One minor request: Could you change the wording here to not refer to constants as "variables"? That was a minor tweak I've been meaning to make, and it seems reasonable to do it here since you're reworking the pages already.
Suggested change
|
||||||
wherever they are used: | ||||||
|
||||||
<!-- mdbook-xgettext: skip --> | ||||||
|
||||||
```rust,editable | ||||||
const DIGEST_SIZE: usize = 3; | ||||||
const ZERO: Option<u8> = Some(42); | ||||||
|
||||||
fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] { | ||||||
let mut digest = [ZERO.unwrap_or(0); DIGEST_SIZE]; | ||||||
for (idx, &b) in text.as_bytes().iter().enumerate() { | ||||||
digest[idx % DIGEST_SIZE] = digest[idx % DIGEST_SIZE].wrapping_add(b); | ||||||
} | ||||||
digest | ||||||
} | ||||||
|
||||||
fn main() { | ||||||
let digest = compute_digest("Hello"); | ||||||
println!("digest: {digest:?}"); | ||||||
} | ||||||
``` | ||||||
|
||||||
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. | ||||||
|
||||||
<details> | ||||||
|
||||||
- Mention that `const` behaves semantically similar to C++'s `constexpr` | ||||||
- It isn't super common that one would need a runtime evaluated constant, but it | ||||||
is helpful and safer than using a static. | ||||||
|
||||||
</details> | ||||||
|
||||||
[1]: https://rust-lang.github.io/rfcs/0246-const-vs-static.html |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this properties slide. This info was originally in the speaker notes and it felt pretty superfluous to me. I think we can remove this content, and move the "More to Explore" section to the speaker notes on the statics slide. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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 | | ||
|
||
# More to Explore | ||
|
||
Because `static` variables are accessible from any thread, they must be `Sync`. | ||
Interior mutability is possible through a | ||
[`Mutex`](https://doc.rust-lang.org/std/sync/struct.Mutex.html), atomic or | ||
similar. | ||
|
||
Thread-local data can be created with the macro `std::thread_local`. |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||||
--- | ||||||||
minutes: 5 | ||||||||
--- | ||||||||
|
||||||||
# Static and Const | ||||||||
|
||||||||
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. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this heading? This slide is about statics, and I think this little introductory header is unnecessary clutter. |
||||||||
|
||||||||
## `static` | ||||||||
|
||||||||
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"; | ||||||||
|
||||||||
fn main() { | ||||||||
println!("{BANNER}"); | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
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. | ||||||||
|
||||||||
<details> | ||||||||
|
||||||||
- `static`, on the other hand, is much more similar to mutable global variable | ||||||||
in C++. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
- `static` provides object identity: an address in memory and state as required | ||||||||
by types with interior mutability such as `Mutex<T>`. | ||||||||
|
||||||||
</details> | ||||||||
|
||||||||
[1]: https://rust-lang.github.io/rfcs/0246-const-vs-static.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to add a redirect in
book.toml
for the old path.