Safety and Soundness in Rust
Rust is designed around safety and soundness. Roughly speaking, safe code is code that doesn't use the
unsafe
keyword, and sound code is code that can't cause memory corruption or other undefined behavior. One of Rust's most important features is the promise that all safe code is sound. But that promise can be broken whenunsafe
code is involved, andunsafe
code is almost always involved somewhere. Data structures likeVec
andHashMap
haveunsafe
code in their implementations, as does any function likeFile::open
that talks to the OS. This leads to a common question: "If Rust can't guarantee that all safe code is sound, how can it be a memory-safe language?" It's hard to give a short answer to that question, so this post is my attempt at a medium-length answer.
Source: Safety and Soundness in Rust, an article by Jack O'Connor.