Safety and Soundness in Rust
&gt; Rust is designed around safety and soundness. Roughly speaking, safe code is code that doesn&#x27;t use the unsafe keyword, and sound code is code that can&#x27;t cause memory corruption or other undefined behavior.<p>I&#x27;d be a bit more precise here. Safety is indeed a property of some particular lexical part of the code, but soundness is really a property of an entire <i>interface</i>. A sound interface never causes UB when you invoke it according to its documented preconditions; an unsound interface may possibly cause UB in some situations even when invoked according to its preconditions.<p>Some take a narrower definition of soundness, treating it primarily as a property of a safe interface: a sound interface that can be invoked from safe code can never cause UB under any circumstances. But personally, I prefer the broader definition, since it also includes unsafe interfaces which don&#x27;t document all the preconditions they rely on.<p>Knowing your interface boundary is an important part of writing unsafe code in Rust. For instance, suppose you have a data type with a method that unsafely makes a certain assumption about its fields. Then you have to be very careful to uphold this invariant in every other safe method that creates or modifies those fields. Furthermore, you have to ensure that no language-level operations on the object as a whole (e.g., swapping, dropping, forgetting, or covariant transformations) can break this invariant. But once you have locally checked all of those things, then you don&#x27;t need to check any other part of the code base to know that your data type is sound.<p>As another example, contrary to what some people say, it&#x27;s not necessarily unsound to have a safe function that can cause UB on some inputs. You just have to ensure that external code cannot call that function (without having previously unsafely promised not to call it incorrectly), and no internal code calls it incorrectly. Here, the interface boundary is the entire section of your code that has access to the function.<p>In particular, you sometimes see this with data types that have to implement safe traits using unsafe operations (for performance or for other reasons). Often, the function to create an object is unsafe, since you have to promise that the rest of your safe code does not invoke those trait implementations in such a way as to cause UB. Practically, you can do this by limiting how much of your code has access to the created object.