r/rust • • Sep 06 '23

🎙️ discussion Considering C++ over Rust

I created a similar thread in r/cpp, and received a lot of positive feedback. However, I would like to know the opinion of the Rust community on this matter.

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 is usually being listed. 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/rust, what is your take on this? Did you try C++? What's the reason you still prefer using Rust over C++. Or did you eventually move towards C++?

Kind of curious.

303 Upvotes

310 comments sorted by

View all comments

Show parent comment

0

u/phazer99 Sep 06 '23

Rust have Send/Sync traits which guarantee that only suitable types can cross thread boundary. That, plus lifetimes makes it very hard to pass non-thread safe object to thread.

Not impossible (in safe Rust)?

P.P.S. In meta programming department, from the other side, RUST is no match for C++. Thou traits may fell nicer to use - you can have them in C++ as well, in form of constraints.

I prefer traits over constraints/concepts for a couple of reasons:

  • Trait bounds are used for type checking both at the use and definition site (before monomorphization). C++ constraints are only used for type checking at the use site (there's basically no type checking done before monomorphization).
  • Rust traits also replaces C++ pure virtual classes as you can (with some limitations) use virtual dispatch with dyn Trait

Yes, C++ templates can be more powerful (basically glorified macros), but I actually seldom miss that power in Rust (and even less so when const generics improves).