4 ms·
There are two languages, “rust” and “unsafe rust”. All code in unsafe blocks is not safe.
by nitsky 6y ago
There are two languages, “rust” and “unsafe rust”. All code in unsafe blocks is not safe.
- cesarb 6y ago> All code in unsafe blocks is not safe. That's a sadly common misconception. The unsafe blocks only enable a few extra language features, like dereferencing raw pointers and calling unsafe functions; if none of these extra language features are used, the code is completely safe. That is, if you have a block of code ({ X }) and just add the unsafe keyword (unsafe { X }), it works exactly the same and is still safe. This also shows that there are not "two languages"; what you called "rust" is a subset of what you called "unsafe rust", not a different language.
- nitsky 6y agoThe first paragraph of the chapter on unsafe rust in TRPL reads: > However, Rust has a second language hidden inside it that doesn’t enforce these memory safety guarantees: it’s called unsafe Rust and works just like regular Rust, but gives us extra superpowers. I disagree that “{ X }” and “unsafe { X }” are the same. The former is provably safe if a bug free rust compiler accepts it. The latter leaves the burden of proof on the programmer.