You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`])
8
8
9
9
The following discusses each of these cases.
10
10
See the [keyword documentation][keyword] for some illustrative examples.
11
11
12
-
[keyword]: ../std/keyword.unsafe.html
13
-
14
12
## Unsafe functions (`unsafe fn`)
15
13
16
14
Unsafe functions are functions that are not safe in all contexts and/or for all possible inputs.
17
15
We say they have *extra safety conditions*, which are requirements that must be upheld by all callers and that the compiler does not check.
18
-
For example, `get_unchecked` has the extra safety condition that the index must be in-bounds.
16
+
For example, [`get_unchecked`] has the extra safety condition that the index must be in-bounds.
19
17
The module defining an unsafe function is responsible for documenting what those extra safety conditions are.
20
18
21
-
Such a function must be prefixed with the keyword `unsafe` and can only be called from inside an `unsafe` block.
19
+
Such a function must be prefixed with the keyword `unsafe` and can only be called from inside an `unsafe` block, or inside `unsafe fn` without the [`unsafe_op_in_unsafe_fn`] lint.
22
20
23
21
## Unsafe blocks (`unsafe {}`)
24
22
25
23
A block of code can be prefixed with the `unsafe` keyword, to permit calling `unsafe` functions or dereferencing raw pointers.
26
24
By default, the body of an unsafe function is also considered to be an unsafe block;
27
-
this can be changed by enabling the `unsafe_op_in_unsafe_fn` lint.
25
+
this can be changed by enabling the [`unsafe_op_in_unsafe_fn`] lint.
28
26
29
27
By putting operations into an unsafe block, the programmer states that they have taken care of satisfying the extra safety conditions of all operations inside that block.
30
28
@@ -39,6 +37,7 @@ For example, Rust provides the language features necessary to implement memory-s
39
37
Rust's type system is a conservative approximation of the dynamic safety requirements, so in some cases there is a performance cost to using safe code.
40
38
For example, a doubly-linked list is not a tree structure and can only be represented with reference-counted pointers in safe code.
41
39
By using `unsafe` blocks to represent the reverse links as raw pointers, it can be implemented without reference counting.
40
+
(See ["Learn Rust With Entirely Too Many Linked Lists"](https://rust-unofficial.github.io/too-many-lists/) for a more in-depth exploration of this particular example.)
42
41
43
42
## Unsafe traits (`unsafe trait`)
44
43
@@ -53,3 +52,7 @@ When implementing an unsafe trait, the implementation needs to be prefixed with
53
52
By writing `unsafe impl`, the programmer states that they have taken care of satisfying the extra safety conditions required by the trait.
54
53
55
54
Unsafe trait implementations are the logical dual to unsafe traits: where unsafe traits define a proof obligation that implementations must uphold, unsafe implementations state that all relevant proof obligations have been discharged.
0 commit comments