3 ms·
Bro. First, unsafe and now assembly. How is Rust a safe language?
by xiaodai 6y ago
Bro. First, unsafe and now assembly. How is Rust a safe language?
- nitsky 6y agoThere 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.
- GolDDranks 6y agoAs you can see, unsafe blocks are required for the assembly block. If you see "unsafe" itself as a problem, you should perhaps read a bit about Rust's philosophy about the matter: https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html