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

24

u/lightmatter501 Sep 05 '23

Anyone who says Rust fixes all C++ issues hasn’t used them both for long enough. There are still things that C++ can do that Rust has no way to express cleanly (ex: bitfields), and things that are much nicer in C++ (some types of metaprogramming).

What I’ve found is that Rust is much better than C++ at creating well-defined interfaces, mostly due to the borrow checker. This makes using libraries much easier because their interface contract is very explicit and mostly enforced by the compiler. Rust also has more powerful metaprogramming than C++ (ex: sqlx and type-checked sql against a db at compile time). Derive macros (C++ needs static reflection for them) are very good at reducing boilerplate.

C++ is also better at getting the last 1% performance out. Rust doesn’t have stable simd and llvm misses some copy elision opportunities.

I would say that if you and everyone you work with already know C++ and can enforce memory safety, Rust is mostly a question of whether the potentially reduced ecosystem is worth the better tooling and developer experience. If you have a bunch of people who are new to systems programming, Rust is MUCH better because it beats good habits into you.

From Rust, I think that C++ should take editions as a way to add new keywords and make syntax changes. Ideally the first one should ban the C preprocessor and enforce modules.

9

u/Sudden_Job7673 Sep 05 '23 edited Sep 05 '23

Ideally the first one should ban the C preprocessor and enforce modules.

Oh, what a dream!

At that point haven't you basically bailed on inter-op between cpp-old and cpp-new though? It feels like a handful of solid "painted ourselves into a corner" features like #include, non-destructive move, etc. have been a big part of why we're talking about Rust and other potential successor languages.

-1

u/germandiago Sep 05 '23

I do not think Rust is a good successor. Cppfront, Carbon and especially Hylo are the better way.

Rust is super complicated for very little return in practice, safety-wise, in most scenarios.

16

u/Sudden_Job7673 Sep 05 '23

Cppfront and Carbon aren't even Beta products. Hylo/Val is stated as a research language. They're pretty much vaperware for the next 2-5ish (?) years and easy to wishcast on.

Rust seems complicated when you have years invested in C++. I picked up social dancing as an adult. Learning new things is hard, I get it.

4

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

Rust does not seem complicated but it IS complicated because borrow checking disables many valid patterns (admittedly it enables zero cost).

Hylo is in research but it is not the kind of research language that comes from nowhere. It has a ton of years of experience in generic and value-based programming practiced in C++ and Swift by Drave Abrahams and Doug Gregor. There are libraries and part of languages enabling these patterns that work today.

It is not a crazy or novel idea by any means. It is also based in all "Better Code" talks from Sean Parent and all lessons learnt since Stepanov STL.

It is true that is is not up to the point where it is not research as such but it is not the kind of free-form research you call pure research either.

The language is already showing a lot, and I mean A LOT of potential simplification over Rust and also without a GC and without a borrow checker. I am not a big fan of the borrow checker as you can see. The borrow checker can detect a lot of stuff, but it puts you a straight jacket even to do simple linked structures And yes, linked structured ARE useful in the areas where I have been working, not academia.

Even in C++ I try to stick mostly to value semantics or unique/const share pointers and very conservatively I use reference escaping.

All the problems Rust guys put on C++ are overly exaggerated for common modern coding standards and a compiler with all warnings as errors.

My opinion is that Rust would be just worth in an OS or a 100% verified critical system. And if you could have the same via something like Hylo, even better bc you get rid of so so much complexity in one shot. Not sure if the performance would be 100% identical but my intuition tells me that pretty close for sure.

Hylo is the result of standarizing good practices in generic programming, value semantics and sensible threading (strucutred threading basically).

As for Carbon and Cppfront, I am not sure how good they will be but if they force better patterns without giving away flexibility and 100% C++ compatibility, I do not see any practical appeal for Rust except in some niches.

1

u/InsanityBlossom Sep 05 '23

So we're talking language complexity here? Ok.

Let's take C++ complexity as 100%.

Rust is (arguably) about 80% complexity of C++ and is missing some of the advanced C++ features.

Now you argue that Hylo, can/will be less complex than Rust AND beat it in features to satisfy the most sophisticated C++ devs?

That's a wishful thinking! Modern languages are complex and always will be.

2

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

No, I am not talking about number of features.

I am talking about sitting down and spending 70% of your time fighting a borrow checker and building a graph in your head because you decided that in your language borrowing stuff all the time is the way to go. And thinking about needing some linked structures and not being able to get through safely or easily, using C libraries and having to do FFIs to claim you are safe (just an illusion), and some other things that put Rust, in practice, to a much narrower safety gap when compared to decent C++. I am not saying it is not an accomplishment. It is. It is just that the complexity it adds is a madness for the practical safety it achieves when, in fact, there are alternatives.

So you spend a disproportionate amount of time thinking about something that with good practices in C++ is going to happen to you once every six months if it ever happens. And that Hylo directly even eliminates and you do not need even to think about. I do not think it is the right default for almost any software. It is just for a narrow niche.

1

u/InsanityBlossom Sep 05 '23

Sure, the borrower checker can sometimes be annoying, but it's not fair to evaluate the whole language with a ton of other features solely based on the fact that some patterns are harder to implement. Yes, graphs and linked lists are a pain, and no one argues that Rust's ownership system doesn't like it. But they are not impossible, there are ways(and crates) to implement them efficiently. And Linked Lists, while being very useful sometimes, are very rare in practice.

Some novice Rust devs think that they should pass everything by reference. This is a pitfall many fall into. It's okay to pass small values sometimes, or use a refcount pointer. This is also true for C++. You don't pass references everywhere, and even if you do, you most likely copy the data at some point ( and C++ loves implicit copies! Try to write a complex C++ program with only passing references around and avoid copying...

The other thing to keep in mind is that the current borrower checker implementation is quite strict and limited, it's not set in stone and work is being done to allow more patterns and ease pain points.

In the end it's a sum of all features that make the language appealing, I can live with the borrower checker yelling at me at times, while enjoying the rest of the features and confidence the compiler gives me.

4

u/germandiago Sep 06 '23

Sure, the borrower checker can sometimes be annoying, but it's not fair to evaluate the whole language with a ton of other features solely based on the fact that some patterns are harder to implement.

Tehy developed all the language around borrow checking and lifetimes to advertise memory safety since the start. Of course this is a central part of the design: APIs must be annotated with lifetimes, safe wrappers must be done in a certain way, some structures are directly outlawed... and yes, it is annoying for what it buys you in most scenarios, I agree with that. In fact I think it was a mistake, TBH. Look at Vale lang (pure research). It tries to fix things without a GC.

And Linked Lists, while being very useful sometimes , are very rare in practice.

In all places where you need persistence and fast swaps you need some kind of linked structures. Server-side this is relatively common, at least in my experience.

Some novice Rust devs think that they should pass everything by reference.

I would avoid it as hell because you get into a lifetimes everywhere hole if you are not careful. In fact, in C++ I try to avoid thinking too much about lifetimes. Once you adopt that style, one of the main selling points of why Rust is better than C++ almost fully vanishes IMHO. There are other things, I know, mainly traits and pattern matching, but especially the first one even creates more friction when adapting OOP APIs from other languages because basically everyone except Go and Rust in the mainstream use other flavors of OOP (Python, C++, Java, C#).

In the end it's a sum of all features that make the language appealing.

I would like to hear what makes Rust so appealing, because I had a propaganda campaign of years about safety and I find it quite inaccurate. I see pattern matching and traits and not much more... for sure there are things, but how many and how important to do a full switch or just code my next project in Rust?

I am not, myself, going to code in Rust "for safety" when I am using a ton of C libraries underneath and find other difficulties. Because I have good strategies and tools to code with something that supports basically all the ecosystem of low-level libraries without FFIs, it is C++.

For sure for an isolated, extra-robust piece of software that is dependency-free you can use Rust as long as you do not have dependencies. But for most software I see it, as we say in Spain, "matar moscas a cañonazos". You can search that: it is translated as "kill flies with cannon shots".

And I really think that is the fundamental problem with Rust: it went the road of needing to wrap FFIs, not being compatible directly with C or C++ and putting lifetimes when lifetimes are more of a problem than a solution for so many average software coding, given that IDEs, tools, linters, warnings exist. I think Carbon or CppFront and/or Hylo, if they succeed, will play nicer and will put Rust in a niche corner.

They obviated backwards-compatibility, developed their utopic model and now it seems that utopias, as usual, do not happen in real world. At least, not as expected. It has contributed some value, for sure. But I do not see such a complicated model as the better way forward for the general case.

→ More replies (11)