|
1 | 1 | # The `unsafe` keyword
|
2 | 2 |
|
3 | 3 | The `unsafe` keyword can occur in several different contexts:
|
4 |
| -unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), and unsafe trait implementations (`unsafe impl`). |
| 4 | +unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), unsafe trait implementations (`unsafe impl`), and unsafe external blocks (`unsafe extern`). |
5 | 5 | It plays several different roles, depending on where it is used and whether the `unsafe_op_in_unsafe_fn` lint is enabled:
|
6 | 6 | - it is used to mark code that *defines* extra safety conditions (`unsafe fn`, `unsafe trait`)
|
7 |
| -- it is used to mark code that needs to *satisfy* extra safety conditions (`unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`]) |
| 7 | +- 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`) |
8 | 8 |
|
9 | 9 | The following discusses each of these cases.
|
10 | 10 | See the [keyword documentation][keyword] for some illustrative examples.
|
@@ -56,3 +56,11 @@ Unsafe trait implementations are the logical dual to unsafe traits: where unsafe
|
56 | 56 | [keyword]: ../std/keyword.unsafe.html
|
57 | 57 | [`get_unchecked`]: ../std/primitive.slice.html#method.get_unchecked
|
58 | 58 | [`unsafe_op_in_unsafe_fn`]: ../rustc/lints/listing/allowed-by-default.html#unsafe-op-in-unsafe-fn
|
| 59 | + |
| 60 | +## Unsafe external blocks (`unsafe extern`) |
| 61 | + |
| 62 | +The programmer who declares an [external block] must assure that the signatures of the items contained within are correct. Failing to do so may lead to undefined behavior. That this obligation has been met is indicated by writing `unsafe extern`. |
| 63 | + |
| 64 | +**Edition differences**: Prior to edition 2024, `extern` blocks were allowed without being qualified as `unsafe`. |
| 65 | + |
| 66 | +[external block]: items/external-blocks.md |
0 commit comments