upvote
Because it lets you constrain the parts that the compiler can’t check and has to trust you on. The alternative is either a langue that can’t do necessary things, or a language that can’t check what could be checked.

With unsafe, you’re telling the compiler “I’ve taken extra care to make sure that what I’m doing is safe and doesn’t break your rules” and the compiler can go ahead and assume that you don’t, in fact, break the rules, and therefore can verify everything else as if the rules never got broken.

In less safe languages, the entire program is “trust me, it’s safe”, while in rust only the parts flagged as unsafe are.

The point is that you should only use unsafe when 1. It’s absolutely necessary for functionality or performance and 2. You have verified and are very certain that the code is correct.

That’s a very useful property to have.

reply
Yes, when a human does it. With an LLM, the "trust me, I took extra care" is extremely doubtful. If LLMs could do that, they might as well use unsafe languages.
reply
Well, Rust has things like miri that can help you write your unsafe code and it's just a command line invocation away.

Obviously you should try to avoid writing unsafe Rust to begin with.

reply
Mu. Invalid question. Rust doesn't disable compiler features. It gives you extra set of footguns when you ask for it.

As for the actual unloaded question, "What's the point of unsafe in Rust?" it is to contain and make it easier to identify sources of UB.

reply
The whole point of the rust rewrite is that bun is now thought to rely on rust’s memory safety features, but that assumption doesn’t hold if everything’s inside an unsafe block.
reply
Only ~4% of code is inside an unsafe block, so the idea is that for new code/contributions, the chance of introducing a new memory-safety bug is an order of magnitude lower.

Maybe in the future the unsafe code will go down to 1%, bringing that to two orders of magnitude.

Of course, only time will tell if that is true or not, but from experience I’d be willing to bet it is.

reply