top | item 38379256

(no title)

chalsprhebaodu | 2 years ago

    Calling “unsafe” as an escape for Rust’s rigid system is the same as saying that C++ can do inline assembly so you can copy/paste infinite optimizations done outside the compiler infrastructure.
That’s not the same at all. Unsafe can be used pragmatically.

    [Regarding UB in C++] So why C++ ignored that possible edge case? Because signed overflow in C++ is undefined behavior and as far as the compiler is concerned, it will never happen. With the edge case reasoned away by the language, the compiler is free to implement that optimization…

    If you compile a similar function in Rust, you will see that Rust will be unable to optimize that expression away because both signed and unsigned overflows in Rust are well defined in the language as a two’s complement… 

    Again, pundits will state that you could have called one of the arithmetic wrapping functions, which will force the compiler to do this optimization. Well you can do many things but here we are measuring the effect of the compiler over two similar blocks of code.
Talk about premature optimization. You’d rather have all of the pitfalls of Undefined Behavior to get an optimization you can achieve in Rust because you’d rather be less explicit?

This article is clearly not directed at me.

For the record, I love C++ and use it daily and will continue to do so. I also like Rust. It can be frustrating at times to have to change the way you think about structuring your program between the two, but that doesn’t mean that one or the other can’t be used effectively and safely.

discuss

order

No comments yet.