Rust Memory Safety
The cluster debates Rust's memory safety guarantees, focusing on the role of 'unsafe' code, its necessity for nontrivial programs, and comparisons to C/C++ where unsafe Rust is argued to be safer despite not being fully memory-safe.
Activity Over Time
Top Contributors
Keywords
Sample Comments
What's a specific memory safety issue Rust has outside `unsafe` code?
Unsafe rust has numerous safety checks above and beyond C++...
Unsafe Rust is much safer than C
Unsafe rust is not a memory-safe language.
Sorry, but this is like saying 'when I am not wrong, I am right 100% of the time'.The devs didn't write unsafe Rust to experience the thrills of living dangerously, they wrote it because the primitives were impossible to express in safe Rust.If I were to write a program in C++ that has a thread-safe doubly linked list in it, I'd be able to bet on that linked list will have safety bugs, not because C++ is an unsafe language, but because multi-threading is hard. In fact,
It has everything to do with unsafe. Because outside unsafe you are forced to use the borrow checker, among other things, preventing you from thrashing memory with 100% guarantee. The number of ways you can thrash memory in c++ can't even by listed, especially not when it comes to object lifetime, so there is no equivalent subset of c++ providing those 100% guarantees. If you do such a mistake just once, the whole execution of the rest of your program might be randomized. Multiply all those
Rust is absolutely a safe language, and in more ways than just memory safety. The only way to get unsafe code in rust is to opt in to unsafety with a keyword that can easily be checked for, so that you can decide for yourself to enforce that no code in your project uses it.And unless you are writing a few specific types of code, using unsafe usually isn't ever necessary at all.The complexity is also very much not an issue in practice. If you rub up against complexity in C++, you might
Rust is memory safe. It does not need this treatment.
Safe Rust eliminates some of the more common memory bugs in C. The bug under discussion was written in unsafe Rust—but even that doesn't obviate the huge advantages Rust has over C. Even unsafe Rust, for instance, has far fewer UB gotchas than C. And with Rust, you can isolate the tricky bits in 'unsafe' blocks and write higher-level logic in safe Rust, giving your code an extra layer of protection. C is 100% unsafe—"unsafe at any speed" as I like to say.
So you're saying Rust is safe because it does exactly what C does?