logoalt Hacker News

tialaramex • today at 4:54 PM • 0 replies • view on HN

> 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.