Rust as a programming language is designed to enforce safety guarantees, especially memory safety, by default. This is achieved through a sophisticated type system and ownership model that ensures safe memory access and prevents common bugs such as null pointer dereferences, buffer overflows, and data races in concurrent code.
By contrast, C++ provides a much greater degree of flexibility and fewer built-in safety mechanisms. Developers in C++ are responsible for managing memory manually with explicit allocation and deallocation, which inherently comes with the risk of creating memory safety issues if not handled correctly.
Rust's "unsafe" keyword allows developers to opt-out of some of these safety checks where necessary. For instance, interfacing with low-level system calls or existing C libraries may require operations that Rust's safety guarantees cannot account for. When using "unsafe" in Rust, the programmer takes on a similar level of responsibility for ensuring the correct handling of memory as they would in C++.
The question is not necessarily whether "unsafe" Rust is more unsafe than C++, but rather how the two languages encourage different approaches to safety. Rust's default mode is safe, with "unsafe" blocks clearly delineated and discouraged unless necessary. This design encourages programmers to maintain safety wherever possible and be more conscious of potential risks when they must use "unsafe" code.
In C++, the entire language could be considered akin to Rust's "unsafe" mode because the compiler does not enforce the same strict safety guarantees. However, modern C++ has introduced features and best practices that promote safer memory management (smart pointers, for example) despite the lack of enforced memory safety at the language level.
Ultimately, the relative safety of code written in "unsafe" Rust compared to C++ depends on the diligence and expertise of the programmers writing it. Well-written C++ code can be quite safe, and careless use of "unsafe" Rust can introduce vulnerabilities. Moreover, while the use of "unsafe" in Rust allows programmers to access potentially risky operations, it also serves as a clear signal to pay extra attention to these blocks of code during code reviews and testing.