@@ -16,10 +16,14 @@ fn index(idx: usize, arr: &[u8]) -> Option<u8> {
16
16
}
17
17
```
18
18
19
- This function is safe and correct. We check that the index is in bounds, and if it
20
- is, index into the array in an unchecked manner. But even in such a trivial
21
- function, the scope of the unsafe block is questionable. Consider changing the
22
- ` < ` to a ` <= ` :
19
+ This function is safe and correct. We check that the index is in bounds, and if
20
+ it is, index into the array in an unchecked manner. We say that such a correct
21
+ unsafely implemented function is * sound* , meaning that safe code cannot cause
22
+ Undefined Behavior through it (which, remember, is the single fundamental
23
+ property of Safe Rust).
24
+
25
+ But even in such a trivial function, the scope of the unsafe block is
26
+ questionable. Consider changing the ` < ` to a ` <= ` :
23
27
24
28
``` rust
25
29
fn index (idx : usize , arr : & [u8 ]) -> Option <u8 > {
@@ -33,10 +37,10 @@ fn index(idx: usize, arr: &[u8]) -> Option<u8> {
33
37
}
34
38
```
35
39
36
- This program is now unsound, and yet * we only modified safe code * . This is the
37
- fundamental problem of safety: it's non-local. The soundness of our unsafe
38
- operations necessarily depends on the state established by otherwise
39
- "safe" operations.
40
+ This program is now * unsound* , Safe Rust can cause Undefined Behavior, and yet
41
+ * we only modified safe code * . This is the fundamental problem of safety: it's
42
+ non-local. The soundness of our unsafe operations necessarily depends on the
43
+ state established by otherwise "safe" operations.
40
44
41
45
Safety is modular in the sense that opting into unsafety doesn't require you
42
46
to consider arbitrary other kinds of badness. For instance, doing an unchecked
0 commit comments