Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Revise unsafe keyword chapter
In light of `unsafe extern`, we need to note that this is another
position that `unsafe` is allowed.  While we're here, let's make some
further revisions to clean up the start of this chapter, including
separating off into a rule these allowed positions.

Co-authored-by: ehuss
  • Loading branch information
traviscross committed Aug 19, 2025
commit 3b88b41d0c8863c4c4b43a68a9ca058a7414ee57
22 changes: 17 additions & 5 deletions src/unsafe-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ r[unsafe]
# The `unsafe` keyword

r[unsafe.intro]
The `unsafe` keyword can occur in several different contexts:
unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), unsafe trait implementations (`unsafe impl`), unsafe external blocks (`unsafe extern`), and unsafe attributes (`#[unsafe(attr)]`).
It plays several different roles, depending on where it is used and whether the `unsafe_op_in_unsafe_fn` lint is enabled:
- it is used to mark code that *defines* extra safety conditions (`unsafe fn`, `unsafe trait`)
- it is used to mark code that needs to *satisfy* extra safety conditions (`unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`], `unsafe extern`, `#[unsafe(attr)]`)
The `unsafe` keyword is used to create or discharge the obligation to prove something safe. Specifically:

- It is used to mark code that *defines* extra safety conditions that must be upheld elsewhere.
- This includes `unsafe fn`, `unsafe static`, and `unsafe trait`.
- It is used to mark code that the programmer *asserts* satisfies safety conditions defined elsewhere.
- This includes `unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`], `unsafe extern`, and `#[unsafe(attr)]`.

The following discusses each of these cases.
See the [keyword documentation][keyword] for some illustrative examples.

r[unsafe.positions]
The `unsafe` keyword can occur in several different contexts:

- unsafe functions (`unsafe fn`)
- unsafe blocks (`unsafe {}`)
- unsafe traits (`unsafe trait`)
- unsafe trait implementations (`unsafe impl`)
- unsafe external blocks (`unsafe extern`)
- unsafe external statics (`unsafe static`)
- unsafe attributes (`#[unsafe(attr)]`)

r[unsafe.fn]
## Unsafe functions (`unsafe fn`)

Expand Down