4 ms·
> "Sure just wrap everything in "Unsafe"... So what is the point?" https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html https://doc.rust-lang.org/book/ch19-
by jodrellblank 2y ago
> "Sure just wrap everything in "Unsafe"... So what is the point?"
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html : "You can take five actions in unsafe Rust that you can’t in safe Rust .. it’s important to understand that unsafe doesn’t turn off the borrow checker or disable any other of Rust’s safety checks: if you use a reference in unsafe code, it will still be checked .. by requiring these five unsafe operations to be inside blocks annotated with unsafe you’ll know that any errors related to memory safety must be within an unsafe block. Keep unsafe blocks small; you’ll be thankful later when you investigate memory bugs."
- bluGill 2y agoYou are confusing best practice for what is done all too often. Unsafe should be small blocks, but I've seen people put unsafe on everything even though it isn't needed thus making it hard to find where it is needed. I'm not a rust programmer, but I'm lead to believe that those people then do things that need unsafe - but a safe option not only exists but would have been easier to write.
- School-Cotton 2y agoFor very low-level stuff (e.g. embedded) you might need a lot of unsafe. For the vast majority of software it’s extremely rare. I worked full time for five years on a Rust project (https://github.com/MaterializeInc/materialize https://github.com/MaterializeInc/materialize) and anecdotally, unsafe code was much less than 1% of the codebase.
- bluGill 2y agoI don't have direct experience, but from what I can gather between others, and my general embedded experience, unsafe in the worse case of embedded would still be just a small percentage of all code. (a device driver which would need unsafe to deal with hardware still has a lot of code that doesn't deal with hardware) There is some rust code out there that is more than 50% unsafe though.
- jodrellblank 2y agoI'm also not a Rust programmer so I can't comment on whether people are overusing it, I was commenting on the suggestion that "if you're using unsafe{} in Rust what's the point in using Rust at all?" implying that it's basically C at that point, and it isn't - there's still a lot of safety checks the compiler does even in unsafe mode.