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

Show parent comments

1

u/mankinskin Sep 06 '23

Sure, but I have never written an unsafe code block. All of the unsafe code is in the libraries I use, and unsafe code is usually designed more carefully and tested extensively. Its much easier to worry about one or two raw pointers across maybe a few methods, than having that as the default. Even if your code has unsafe blocks, 99% of the pointers you are using are still compile time checked references.

1

u/Full-Spectral Sep 06 '23

This is such a common issue. People somehow think that Rust code must be full of unsafe code. Well, sadly, a lot of it is but not because it has to be. You can read in this very discussion a lot of people who are treating it like C++. But, for the most part, outside of low level libraries, there's going to be hardly any, if any.

And, though it would be nice to be 100% safe down to the transistors, having 98% of my own code safe (and even most of that 2% really only technically unsafe), is a huge win over the unsafe by design C++ equivalent, where it's constantly having to waste brain cycles being sure I don't sure myself in the foot.

Even if I manage to not shoot myself in the foot, I still wasted those brain cycles that could have been spent on application logic.

1

u/mankinskin Sep 06 '23

Exactly. Its like switching from a car to a plane. Its a different mindset of writing code. You are not thinking about how the CPU runs through your code basically blind and how everything has to fit perfectly to not crash. You are just thinking about the problem you are solving. When I write Rust code I can expect it to run. The only reason something might crash is because I run into a panic which means I misunderstood the functions I was calling or have a business logic error. Sure those things still happen. But the base language is literally foolproof because the compiler checks for everything that could go wrong. You simply don't run into errors because of "nonsense" anymore.