Skip to content

Instantly share code, notes, and snippets.

@lifthrasiir
Created November 6, 2014 04:43
Show Gist options
  • Save lifthrasiir/952e487366f07d543478 to your computer and use it in GitHub Desktop.
Save lifthrasiir/952e487366f07d543478 to your computer and use it in GitHub Desktop.
Safe and Unsafe

The terms safe and unsafe are used for many related but slightly differing meanings in Rust.

  1. 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.
  2. The memory safety: The code does not violate the memory safety. The compiler may or may not be able to prove that.
  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment