Skip to content
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

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

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.

- [Static and Const](user-defined-types/static.md)
- [Const](user-defined-types/const.md)
- [Properties](user-defined-types/properties.md)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [Static and Const](user-defined-types/static.md)
- [Const](user-defined-types/const.md)
- [Properties](user-defined-types/properties.md)
- [Statics](user-defined-types/static.md)
- [Constants](user-defined-types/const.md)

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)
Expand Down
39 changes: 39 additions & 0 deletions src/user-defined-types/const.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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Constant variables are evaluated at compile time and their values are inlined
Constants are evaluated at compile time and their values are inlined

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
18 changes: 18 additions & 0 deletions src/user-defined-types/properties.md
Copy link
Collaborator

Choose a reason for hiding this comment

The 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`.
90 changes: 0 additions & 90 deletions src/user-defined-types/static-and-const.md

This file was deleted.

38 changes: 38 additions & 0 deletions src/user-defined-types/static.md
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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++.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `static`, on the other hand, is much more similar to mutable global variable
in C++.
- `static` is similar to mutable global variables in C++.

- `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