The terms safe and unsafe are used for many related but slightly differing meanings in Rust.
- The provable memory safety: The compiler can prove that the code does not (or, cannot) violate the memory safety. Here the memory safety refers to the absence of access to the non-allocated or invalid pointers and the absence of dangling (allocated-then-never-deallocated) pointers.
- The memory safety: The code does not violate the memory safety. The compiler may or may not be able to prove that.
- The behaviorial safety: The code does not violate the user's expectation. The exact definition may vary, but this includes the memory safety, no unexpected integer overflows, no unexpected out-of-bounds condition, and so on.
Rust provides the provable memory safety, and allows users to mark the code with non-provable memory safety as unsafe
. Rust in general does not provide a strict mechanism to eliminate the behaviorial unsafety, but both the standard library and 3rd-party library writers are recommended to do the best to avoid that. When it's not possible or feasible, they are still required to ensure that the behaviorial unsafety cannot turn into the memory unsafety.