logoalt Hacker News

uecker • today at 3:28 PM • 2 replies • view on HN

This "all C++ is unsafe" is similarly exaggerated Rust marketing claim. It is logically equivalent to "all Rust code is unsafe because it may contain unsafe" and nonsense for the same reason. The only claim you can make is that it is easier to screen for the unsafe parts because it is marked more clearly. I would agree with this much more limited statement.


Replies

tialaramex • today at 4:54 PM

> This "all C++ is unsafe" is similarly exaggerated Rust marketing claim

It really isn't, and because I have no doubt you made this assertion genuinely that is itself a problem for their language.

C++ APIs with broad - and thus actually safe - contracts are very rare. Most of the time what we'll find is that there were a few "obvious" assumptions narrowing a seemingly wide contract, an iterator that's assumed to be non-null, a parameter that's expected to be in bounds, or to have this or that desirable property - and if it doesn't while Rust would reject the program alas in C++ it's just Undefined Behaviour so too bad you lose.

The quintessential example is sorting. Maybe we've got a billion Doodads and we sort them. What happens if our sort order is incoherent nonsense, either because our type doesn't actually have a total order or because we provided an explicit ordering rule which was not coherent? In Rust the answer is that this remains safe, since there was no coherent order we can't really say that the Doodads are now "sorted", but the same billion Doodads are there. In C++ that's Undefined Behaviour. Anything could happen. In the real world maybe some of the C++ Doodads are corrupted, or duplicated, or the total number of Doodads changes, maybe arbitrary code is executed, it's a horror show.

IshKebab • today at 3:50 PM

The fact is that in practice Rust essentially eliminates all memory safety issues. You can describe that however you want, but most people are perfectly happy with "Rust is memory safe", and you know what they mean when they say it.

➕ show 1 reply