4 ms·
Because performance improvements are not why `unsafe` exists. It exists to disable various conservative compile-time checks in order to let you write code that
by umanwizard 7y ago
Because performance improvements are not why `unsafe` exists. It exists to disable various conservative compile-time checks in order to let you write code that the compiler is unable to prove correctness properties about.
One reason to do this might be performance. An example: if you can prove lifetime properties but the compiler can’t, you might be able to just use raw pointers without the borrow checker, which is unsafe. This can let you avoid the performance penalty of smart pointers or cloning.
But there are other reasons to do it, too. Some things simply can’t be reasoned about by the compiler, like calling external C code, so they always need `unsafe`.
- steveklabnik 7y ago> It exists to disable various conservative compile-time checks There is a subtle but important distinction here. Unsafe does not disable any compile time checks. It adds additional constructs which are not checked.
- umanwizard 7y agoThank you, you are right. It enables certain constructs, e.g. dereferencing raw pointers, which have fewer compile-time checks than safe constructs, e.g. references.