Skip to content

Commit 486408f

Browse files
Improve note on unsafe functions and unsafe blocks (#4204)
Improve note on unsafe functions and `unsafe` blocks See discussion in #4147, #4148. --------- Co-authored-by: Matthias Totschnig <[email protected]>
1 parent 8a0eee2 commit 486408f

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/ch20-01-unsafe-rust.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,11 @@ With the `unsafe` block, we’re asserting to Rust that we’ve read the functio
186186
documentation, we understand how to use it properly, and we’ve verified that
187187
we’re fulfilling the contract of the function.
188188

189-
> Note: In earlier versions of Rust, the body of an unsafe function was treated
190-
> as an `unsafe` block, so you could perform any unsafe operation within the
191-
> body of an `unsafe` function. In later versions of Rust, the compiler will
192-
> warn you that you need to use an `unsafe` block to perform unsafe operations
193-
> in the body of an unsafe function. This is because Rust now distinguishes
194-
> between `unsafe fn`, which defines what you need to do to call the function
195-
> safely, and an `unsafe` block, where you actually uphold that “contract” the
196-
> function establishes.
189+
To perform unsafe operations in the body of an unsafe function, you still need
190+
to use an `unsafe` block just as within a regular function, and the compiler
191+
will warn you if you forget. This helps to keep `unsafe` blocks as small as
192+
possible, as unsafe operations may not be needed across the whole function
193+
body.
197194

198195
#### Creating a Safe Abstraction over Unsafe Code
199196

@@ -550,5 +547,6 @@ Rust’s official guide to the subject, the [Rustonomicon][nomicon].
550547
[the-slice-type]: ch04-03-slices.html#the-slice-type
551548
[reference]: ../reference/items/unions.html
552549
[miri]: https://github.com/rust-lang/miri
550+
[editions]: appendix-05-editions.html
553551
[nightly]: appendix-07-nightly-rust.html
554552
[nomicon]: https://doc.rust-lang.org/nomicon/

0 commit comments

Comments
 (0)