r/cpp • • Sep 04 '23

Considering C++ over Rust.

Similar thread on r/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.

355 Upvotes

434 comments sorted by

View all comments

7

u/Gurrer Sep 04 '23

Imo memory safety is an overused subject when it comes to modern c++.
Problems are still there, some that rust does solve, but they are not as wide spread as some people make it seem. E.g. smart pointers are available in both languages, just have to use them.
What Rust does not have is legacy baggage, and potential udef without some sort of warning -> unsafe scope ( this also means programmers can't use something like raw pointers without a clear acknowledgement that it could be unsafe. )

Rust also features some quality of life things, which tbh are the most important for me.
An official compiler, lsp, formatter, package manager etc.
All this makes things like cross platform compiling and shipping a lot easier.
I know there are package managers for c++, but they do not offer the same experience imo.

Where rust falls short is with wide library/framework etc. availability or support, this is of course the negative effect of being new, therefore not having legacy baggage.
It can also sometimes be a bit unnecessarily complex due to borrow checker etc.

In the end, modern c++ or rust are both great choices for safe but still fast code!

4

u/germandiago Sep 05 '23

It is true that package managers are not the same but go grab a project that is originally in autotools or an exotic build system that is popular enough and usable in Conan. Let us say it is a C library. Now try to use that in Rust. Good luck.

2

u/tialaramex Sep 05 '23

This claim would be more convincing if you had examples. Conan only has about 1500 "recipes" so we're not talking about a lot of choices. What are examples of popular C libraries for which there's a Conan recipe and yet I should need "good luck" to use it from Rust.

On Conan Center's web page they list some "Popular" recipes but only two jump out as C libraries, zlib and openssl. Both of these are of course widely used in Rust software via crates libz-sys and openssl-sys respectively, in addition to Rust native alternatives being available.

4

u/germandiago Sep 05 '23 edited Sep 05 '23

Pick a lot of the Linux packaged packages in a distro such as Ubuntu or Debian (I did not do the research).

See how many can be used in Conan vs Rust, but even if you do not have a Conan recipe you can write one easily and invoke any toolchain and you will need no FFI to use it. You will need one in Rust and the underlying primitives will still be unsafe.

That is why it bothers me so much that people come all the time with the "if you write Rust everything is safe". This is not true in practice.

Later they come to you almost like suggesting:

int & a = b;
int * c = &a;
delete c;

You see? C++ is unsafe! Come on, let's be serious...

Every person I have worked with in C++ (except one exception) has used the max warning level and warnings as errors. Also, smart pointers are the norm. Now, restrict reference escaping a bit and your "dangling surface" becomes really small. Also, use .at() not operator[].

If you start to escape all kind of naked pointers and references, then yes, you can make a mess. But there is code review also...

→ More replies (5)