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/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)