Skip to content

Commit f5787fe

Browse files
committed
Update text to mention 2024 edition.
1 parent 4a07baf commit f5787fe

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

Diff for: src/cargo/rust-ecosystem.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Key points:
3232
- Dependencies can also be resolved from alternative [registries], git, folders,
3333
and more.
3434

35-
- Rust also has [editions]: the current edition is Rust 2021. Previous editions
36-
were Rust 2015 and Rust 2018.
35+
- Rust also has [editions]: the current edition is Rust 2024. Previous editions
36+
were Rust 2015, Rust 2018 and Rust 2021.
3737

3838
- The editions are allowed to make backwards incompatible changes to the
3939
language.

Diff for: src/unsafe-rust/mutable-static.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static mut COUNTER: u32 = 0;
2222
2323
fn add_to_counter(inc: u32) {
2424
// SAFETY: There are no other threads which could be accessing `COUNTER`.
25+
#[allow(static_mut_refs)]
2526
unsafe {
2627
COUNTER += inc;
2728
}
@@ -31,6 +32,7 @@ fn main() {
3132
add_to_counter(42);
3233
3334
// SAFETY: There are no other threads which could be accessing `COUNTER`.
35+
#[allow(static_mut_refs)]
3436
unsafe {
3537
println!("COUNTER: {COUNTER}");
3638
}
@@ -40,12 +42,16 @@ fn main() {
4042
<details>
4143

4244
- The program here is safe because it is single-threaded. However, the Rust
43-
compiler is conservative and will assume the worst. Try removing the `unsafe`
44-
and see how the compiler explains that it is undefined behavior to mutate a
45-
static from multiple threads.
46-
47-
- Using a mutable static is generally a bad idea, but there are some cases where
48-
it might make sense in low-level `no_std` code, such as implementing a heap
49-
allocator or working with some C APIs.
45+
compiler reasons about functions individually so can't assume that. Try
46+
removing the `unsafe` and see how the compiler explains that it is undefined
47+
behavior to access a mutable static from multiple threads.
48+
- Rust 2024 edition goes further and makes accessing a mutable static by
49+
reference an error by default. We work around this in the example with
50+
`#[allow(static_mut_refs)]`. Don't do this.
51+
- Using a mutable static is almost always a bad idea, you should use interior
52+
mutability instead.
53+
- There are some cases where it might be necessary in low-level `no_std` code,
54+
such as implementing a heap allocator or working with some C APIs. In this
55+
case you should use pointers rather than references.
5056

5157
</details>

Diff for: src/unsafe-rust/unsafe-functions/rust.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ preconditions to avoid undefined behaviour.
1111
/// The pointers must be valid, properly aligned, and not otherwise accessed for
1212
/// the duration of the function call.
1313
unsafe fn swap(a: *mut u8, b: *mut u8) {
14-
let temp = *a;
15-
*a = *b;
16-
*b = temp;
14+
// SAFETY: Our caller promised that the pointers are valid, properly aligned
15+
// and have no other access.
16+
unsafe {
17+
let temp = *a;
18+
*a = *b;
19+
*b = temp;
20+
}
1721
}
1822
1923
fn main() {
@@ -35,9 +39,9 @@ fn main() {
3539
We wouldn't actually use pointers for a `swap` function --- it can be done
3640
safely with references.
3741

38-
Note that unsafe code is allowed within an unsafe function without an `unsafe`
39-
block. We can prohibit this with `#[deny(unsafe_op_in_unsafe_fn)]`. Try adding
40-
it and see what happens. This will
41-
[change in the 2024 Rust edition](https://github.com/rust-lang/rust/issues/120535).
42+
Note that Rust 2021 and earlier allow unsafe code within an unsafe function
43+
without an `unsafe` block. This changed in the 2024 edition. We can prohibit it
44+
in older editions with `#[deny(unsafe_op_in_unsafe_fn)]`. Try adding it and see
45+
what happens.
4246

4347
</details>

0 commit comments

Comments
 (0)