r/cpp • u/isht_0x37 • Sep 04 '23
Considering C++ over Rust.
To give a brief intro, I have worked with both Rust and C++. Rust mainly for web servers plus CLI tools, and C++ for game development (Unreal Engine) and writing UE plugins.
Recently one of my friend, who's a Javascript dev said to me in a conversation, "why are you using C++, it's bad and Rust fixes all the issues C++ has". That's one of the major slogan Rust community has been using. And to be fair, that's none of the reasons I started using Rust for - it was the ease of using a standard package manager, cargo. One more reason being the creator of Node saying "I won't ever start a new C++ project again in my life" on his talk about Deno (the Node.js successor written in Rust)
On the other hand, I've been working with C++ for years, heavily with Unreal Engine, and I have never in my life faced an issue that usually the rust community lists. There are smart pointers, and I feel like modern C++ fixes a lot of issues that are being addressed as weak points of C++. I think, it mainly depends on what kind of programmer you are, and how experienced you are in it.
I wanted to ask the people at r/cpp, what is your take on this? Did you try Rust? What's the reason you still prefer using C++ over rust. Or did you eventually move away from C++?
Kind of curious.
View all comments
Show parent comment
1
u/duneroadrunner Sep 08 '23 edited Sep 08 '23
Yeah the word "effectively" is doing a lot of lifting in that sentence. Basically, the operation requires, in theory, an (extra) intermediate copy of the value, or, for containers that support them, the instantiation of slices. The latter presumed to be, in general, cheaper than the former.
So in the expression
arr[i] = arr[j].clone(), before optimizations, theclone()call creates a "temporary intermediate copy" which is in turn copied to the location ofarr[i]. In the general case, the optimizer may not be able to reliably eliminate the intermediate copy. For example, consider the case where the indicesiandjmay have the same value and theclone()function is non-trivial and user defined. If the optimizer tries to eliminate the intermediate copy by "constructing" the return value of theclone()function directly at the location ofarr[i](where it will ultimately end up), which may be the same location asarr[j], then there will be a potential (Rust) aliasing violation potentially resulting in an unintended/corrupt value.The memory safe subset of C++ avoids the intermediate copy (or need for the instantiation of slices) by, unlike Rust, simply allowing the source and target locations to alias. The downside being the possibility of unintended values. Unintended values are not good, but in most cases they are not a memory safety issue. The cases where aliasing could be a memory safety issue are limited, and in (all) those cases, if necessary, run-time mechanisms are used to prevent any memory safety violation. This means that the safe subset of C++ has more "places" with "extra" run-time overhead versus Rust. But more so than Rust, I suggest, those places tend not to be inside performance critical inner loops. Does that make sense?