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.
4
u/germandiago Sep 06 '23 edited Sep 06 '23
Here you have basically a whole recompilation every time and, for what you say in the second comment, to rebuild from source every time... not very practical IMHO. It can change over time, I hope it improves, but nowadays things like Conan + Artifactory let you cache things and this is, IMHO, necessary for practical CI.
Meson build system also. And that is a problem for CI unless you can cache things. The amount of waste is enormous. Conan wins hand down here. I tell you about this because this would have been a problem to me I think. It slows down the whole pipelines (quite repetitive) by a lot.
What I argue here is how much safer and at which cost. If we take all C/C++ software made, then Rust will win by a long margin bc this includes Win32 APIs, COM and other things full of pointers and stuff like that. But if you pick software codebases done in the last, let us say, 8-10 years with most modern practices in (not all of them, but a representative sample), smart pointers, etc.? What would be the metric? I would say, but I do not have data, same as you, that the safety gap is not much safer and it will be probably something between a bit safer or almost same safety bc in the real world Rust is going to invoke C libraries and C++ using good practices (which is what I use, I do not measure things against what I do not do)... I never had a leak or a crash of use-after-free for a very long time. I mean years. Except, I must admit, once I made a mistake in multithreaded code that had some low-level simd intrinsics. But that could happen to you in any language, bc at the end you will have to enter unsafe land.
I did not mean there are not. But since Rust is "the safe language", not the others, I do not see it is the safe language they advertise. It is the safe language if: you do not invoke C or C++, you stick to Rust and you do not use unsafe. So you cannot do the equivalent of a reinterpret_cast or mess with SIMD and alignment, for example.
Write all your software that way and you are supposed to be safe. You will be safe, and, as a side-effect, totally impractical as of today.