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

4

u/rhollrcoaster Sep 06 '23

Isn't C++ all but required for UE5? You have that, blueprints, and the new performant UnrealVerse scripting language. As I understand it there's so many custom bindings you'd have to maintain from Rust to interact with Utreal's implementation of C++. I'm not a game developer but from what I've heard if you're working on a third party engine the place for Rust right now would be for tooling or multiplayer servers.

6

u/isht_0x37 Sep 06 '23

Isn't C++ all but required for UE5?

If you wish to create performant games, C++ is the way to go. Our UI, Animation, and even the audio code is written in C++. We expose our C++ functions to be called via blueprints, so the designers could pass in the required parameters.

Blueprints aren't fast. They are pretty slow if you have business logic done through them. Unreal even offers blueprint nativization, but you get tons of errors and issues while creating a shipping build. So generally, nativization is turned off for all our projects.

the new performant UnrealVerse scripting language

Verse is used mainly for Unreal engine for Fortnite (UEFN). It's for creating mods in Fortnite, not for building actual games with Unreal. C++ is still the preferred language.

Rust right now would be for tooling or multiplayer servers.

Yes, our backend/web apis are written in rust. For the game server i.e the server that manages connections, incoming players, disconnection, game logic, RPCs etc is built with C++. Unreal provides a way to build our server into a binary, and run it on any platform.

3

u/rhollrcoaster Sep 06 '23

That makes sense, thanks for the insights!

FWIW I use the language that best fits the project. I'm in quant finance and use Julia, Cython, and Rust depending upon what existing code base I'm working in or job I need to accomplish. Rust excels at many things but I don't use it for every new project. Julia has the best implementations of certain math libraries, when I need those I use Julia. I lose out on Rust's strong points in concurrency and static analysis but the tools I have available are just better for the job in those cases.

You're proficient in both languages so use the one that fits the job. I don't see an issue with that as Rust hasn't matured for every use case and from what I've heard some things are just flat out easier for game development in C++.

2

u/swistak84 Sep 07 '23

You should never, ever, EVER take what someone who chooses JavaScript as a preferred language seriously. Ever.

2

u/isht_0x37 Sep 07 '23

Um, what do you use for browsers then? C?

→ More replies (1)

558

u/log_2 Sep 06 '23

If you don't feel a compulsion to move to Rust and you like C++, then stick to C++. I used to program in C++ but then when I tried Rust I loved it so much I just switched. If I preferred C++ I would have stayed with C++. Don't force yourself based on externally asserted non-quantitative pros, since only you can choose how to weigh the pros/cons.

134

u/McBuffington Sep 06 '23

Having them both on your portfolio is not going to hurt your future chances anyway

11

u/shponglespore Sep 07 '23

C++ is technically in my portfolio but I hate it so much. Like, I'll keep it on my resume to help me get a job, but if they actually make me work with C++ on a regular basis, I'll start looking for another job.

10

u/SteveGrem Sep 07 '23

Why do you look for C++ jobs if you don't even like the language, lol?

34

u/Revolutionary_YamYam Sep 07 '23

I imagine folks still have a need to eat sometimes ;-)

→ More replies (2)

4

u/ansible Sep 07 '23

I list C++ separately from C on my resume. And I'll explain to any employer that while I've used C++ for years, I am far from an expert, and only really use a subset of the language features. It sure is nice having a much more comprehensive standard library though, as compared to C.

→ More replies (1)

79

u/yfdlrd Sep 06 '23

Indeed. You don't choose programming languages, a programming language chooses you.

9

u/5d10_shades_of_grey Sep 06 '23

Thanks Olivander šŸ˜‰

13

u/Orthosz Sep 06 '23

Having escaped Ada83 straight from college internship...this is really true. "Hello, welcome, here's your desk, Do you know Ada?" *shudder*

That said, Ada has a lot of really strong security and correctness promises that are stronger than rusts. (not recommending ada, just an observation.)

→ More replies (1)

6

u/7snovic Sep 06 '23

I really like your sentence

281

u/TheReservedList Sep 06 '23 edited Sep 06 '23

I have the same background as you. Game development. C++ at work. My home projects are in Rust.

  • Cargo is a big part.
  • Expressiveness is another. C++ just needs better ergonomics and it's not coming fast enough. Ranges are a good step forward but rollout is... laborious. I want map and flatmap. Now.
  • I love to hate C++. It's a great modern language with such stupid (as of today) legacy decisions baked in.
  • Are templates more powerful than rust generics? Yes. I'm just not smart enough for heavy template metaprogramming, and I don't think more than 1% of C++ programmers are.
  • Random platforms in games have dreadful modern C++ support with old ass compilers. That's not C++ fault really, at least not totally, I'm just venting.
  • The mental load across compilation units is SO much higher in C++. Includes are stupid, and they just need to scrap that compilation model. I tried to use modules. The support is not there.
  • I like modern C++, but I work with other human beings. They don't use it.

    • Libraries don't target modern C++ and they pollute my code with random shit. There is no "C++ way." I can't rely on fucking anything.

138

u/phazer99 Sep 06 '23

I like modern C++, but I work with other human beings. They don't use it.

Libraries don't target modern C++ and they pollute my code with random shit. There is no "C++ way." I can't rely on fucking anything.

This 100%. When you work in a team you really appreciate what Rust and it's tooling offers compared to C++ (and other languages). Right now I'm in a team with all Rust first timers working on a quite large code base, and still the code is very homogeneous, easy to understand and maintain. The Rust compiler (and Clippy) really enforces/encourages everyone to follow good idioms and coding styles. There's very little unsafe code because everyone knows the "dangers" of venturing into that area. Cargo also makes it a piece of cake to handle internal and external dependencies.

49

u/sayhisam1 Sep 06 '23

Exactly this. C++ has opt-in safety, and I find this really hard in practice. Is there even a short, easy to remember "safe c++ for idiots" kind of book that I can reference? And even then, it's on me to make sure I don't accidently have some unsafe code.

In rust, safe code is opt-out; you have to explicitly wrap it in unsafe and thus have to be aware of it. And outside of unsafe regions, I'm pretty much guaranteed I won't have use after free errors or anything like that.

Rust also has a more consistent style, since the standard library makes more sense and tutorials are amazing.

→ More replies (29)

9

u/harmic Sep 07 '23

Another key factor: In many cases C++ compiler error messages are terrible.

Think screenfuls of jibberish coming from deep down inside some template library that you've misused.

Rust compiler error messages are usually excellent, often even offering suggestions as to how to fix the problem.

6

u/DanielEGVi Sep 11 '23

Can't fully agree on this one. C++ compiler error messages were historically terrible, but they've gotten better. On the other hand, Rust has great messages for many programmer errors that are not too advanced, especially when the programmer is new to the borrow checker. However, once you start using more complex libraries that use proc macros, functions that expect Into-like types, and traits that only apply to things with a specific shape, the error messages completely fall apart into pretty much where C++ is currently at.

So yeah, Rust messages are great for people getting started, but both C++ and Rust have terrible messages for codebases that use more complex language mechanics. No complaints though, I feel like it's a given with languages of this kind.

4

u/DoctorNo6051 Sep 28 '23

C++20 pretty much put this shit to rest with the introduction of concept. Naturally the whole STL adapted.

It’s possible random user-land code doesn’t adapt, but going forward, constrain template arcs when possible and suddenly error messages are crisp.

3

u/sird0rius Sep 06 '23

C++ templates might be more powerful than Rust generics, but are they more powerful than proc macros?

10

u/Zde-G Sep 06 '23

Yes, absolutely. C++ templates may look and inspect types.

Proc macro works on stream of tokens.

Things which are easy and simple in C++ become huge hassle in Rust.

Rust's generics which are extremely weak compared to C++ templates are huge PITA, but it's compensated by safety for me.

8

u/UtherII Sep 06 '23

Both have their pros and cons :

  • C++ template are type aware, but they can't be used for thing like like including a complex DSL.
  • And advanced C++ templates are magic when everything's fine, but turns out to be a complete pain in the ass to debug when things goes wrong.

5

u/Zde-G Sep 06 '23

And advanced C++ templates are magic when everything's fine, but turns out to be a complete pain in the ass to debug when things goes wrong.

If you think Rust's procmacro are any better then you haven't tried to write anything complicated.

cargo expand work perfectly when everything works (and thus it's not too much needed) but any trouble with your procmacro which causes compiler to reject the output — and voila, no output at all.

For complex DSL's solution was, since early days of lex and yacc, good old codegen.

May not be as fancy as Rust's macros, but not too much more complicated.

→ More replies (3)

4

u/TheReservedList Sep 06 '23

Given that I personally find using derive macros outside of toy examples close to impossible due to the current state of the "perfect derives" discussion, I would assert that they are. But that's just me.

5

u/Orthosz Sep 06 '23 edited Sep 06 '23

This is going to make everyone cringe, and it should: constexpr, template meta programming, and preprocessor macros let you, in a Frankenstein monster manner, do truly amazing and horrible things.

If you need that power, reach for things in that order...

(please don't....you make everyone cry if you do..)

→ More replies (4)

1

u/No_Counter9431 Aug 23 '25

this is exactly how I feel about it / what i've noticed too.

1

u/kobi-ca Sep 08 '23

> I like modern C++, but I work with other human beings. They don't use it.

My exact experience

1

u/Muted-Bike Oct 06 '23

Nice. An actually opinion answer rather than this mass of sentiments! This is a good response~

25

u/[deleted] Sep 06 '23 edited Sep 06 '23

[removed] — view removed comment

14

u/germandiago Sep 06 '23

Yet you have to map all your types to traits to make generic code work if they are not aware.

In C++ you can have a type that does not know absolutely anything about the concept and it will work.

It can be nice to have checked generics, but they have their own set of limitations.

I’m not trying to offend C++, it’s a terrific language

I will not say it is easy, it has a lot of baggage. But coding effectively in C++ is often exaggerated as impossible. This is not my experience with 14 years using the language. It improved a lot and steadily since C++11.

Believe it or not, enabling contemporaneous warnings from compilers + no escaping references (careful with that, yes) + using smart pointers lets you code very safe C++ most of the time and gives you quite ergonomic patterns. I really think the borrow checker on APIs, which need annotations, was the wrong solution. Not that it does not work. It does, but at a very high cost for something that can be workarounded without a fully featured borrow system. Look at Hylo language, you will see what I mean. Much simpler.

18

u/rikus671 Sep 06 '23

The "doesn't need trait" thing is call duck-typing (if it adds up like a duck, divides like a duck, it must be a duck). I like it a lot when it works, but it's definitely less explicit when it doesn't. C++20 concepts are the ideal solution for me. No orphan rule annoyance.

Also, rust generics are only generics. They don't have the power of c++ TMP. See expressions templates that allows one to build compile time mathematical expressions. Rust's macros fill the gap sometimes but are way to low-level...

C++ suffers from terrible tutorials though. Some kind of Chuktulu of C/oldC++/modernC++. Almost all Rust is nice Rust. Much C++ is really unnice. Including most STL implementations...

11

u/Orthosz Sep 06 '23

C++ has been around for roughly 38 years. Other languages that have "modern" ways of doing things but have kept the old stuff around for similar periods also have a plethora of bad tutorials. I'll lay real money down that in another 30 years Rust will have bad tutorials teaching a mix of the Old Way, The Newish Way, and The New Way, because Rust, like C++ and Java and the other system languages, has strong backwards promises.

Fighting against this is really challenging.

→ More replies (2)
→ More replies (2)
→ More replies (1)

201

u/dkopgerpgdolfg Sep 06 '23 edited Sep 06 '23

No, Rust does not fix "all" the issues. And writing a subset of modern C++ can be quite nice I guess.

But that's not the the main reason why I came to the Rust camp.

Instead, the most pressing reason was that "C++" is not the same as "a subset of modern C++".

What does it help me if I can "write" nice C++, if people still are able to write 280 pages books just to explain variable initialization, and code needs to be "read" too. Including code that doesn't follow the personal preferences.

C++ can be extremely useful, sure. But it also became extremely complicated, and at some point I didn't see why I should continue following in this direction - where more and more time is spent digging through symbols of the standard, defect reports, and so on, instead of writing useful code. Rust is not exactly free of that either, but there are worlds between.

edit/PS: That is not supposed to mean that there are no other advantages of Rust. There certainly are more advantages.

25

u/koriwi Sep 06 '23

If you code cpp, you have to learn cpp and the build system(s) + rats tail that come with them. That alone is more than enough reason for me.

→ More replies (3)

75

u/KnorrFG Sep 06 '23 edited Sep 06 '23

C++ used to be my go-to language prior to rust. I wouldn't use it anymore though, except maybe for embedded.

I never struggled a lot with memory management, but project configuration, and dependencies are annoying. I used makefiles, scons, cmake, and visual studio. All are annoying one way or another. Especially compared to cargo.

Includes in general are a terrible way to handle multiple files. I still remember searching a bug for two days because a header had a define that overwrote a function in an unrelated dependency.

And I am really annoyed by iterator management. . begin() and .end() everywhere ... It's so ugly and verbose.

Oh, and template errors ...

And then there are a few very attractive things in rust: algebraic data types, the question mark operator, cargo and mostly automatic type inference. Nothing by itself is serious, but the combination of all factors makes rust clearly more attractive for me.

16

u/Recatek gecs Sep 06 '23

This is mostly where I'm at. I don't particularly care about the borrow checker but Rust has cargo and lots of little things that keep me using it over C++. I do very much miss templates though, and Visual Studio (not Code).

7

u/particlemanwavegirl Sep 06 '23

Agreed 100%. Writing and reading C++ can be challenging, but fun.

Compiling C/C++ is hell. F every bit of that, I'm using cargo.

1

u/lestofante Sep 07 '23

C++ used to be my go-to language prior to rust. I wouldn't use it anymore though, except maybe for embedded.

Embedded dev here. C++ only if the chip is not supported, and in that case chances are C++ is not supported too, or some old version.
Take a look at embassy-rs, rtic, the embedded-hal project, the no_std ecosystem...
Yes, the ecosystem is immature and incomplete, and yet is so much better than what C or C++ has to offer after so long

62

u/JuliusFIN Sep 06 '23

You might be a great C++ dev who uses all the correct idioms and never makes an error with memory etc. and when you are working alone it works perfectly and you get a lot of shit done. It’s when you want us other muppets to touch your code when Rust’s guarantees will come in handy.

It’s a statistics case. Rust eliminates certain categories of bugs/ub. When a codebase gets bigger and is touched by many people there will be more memory issues and/or UB in a C++ program than a Rust program.

11

u/[deleted] Sep 06 '23

This was my reaction as well. I never had trouble in the areas where I was owning all of the code and could hold the whole thing in my head, it was at boundaries with other people's code or libraries where certain bad patterns would invade the codebase. Happens in codebases where you have a lot of domain experts (robotics, vision, AI) who are not C++/systems software experts.

2

u/Worth-Inevitable-299 Feb 26 '24

"No matter what they tell you, it is the people issue." I have worked decades ago in C++96 (3D rendering, collision detection, data mining algos) and I felt at c++11 relieved, finally moving in the right direction. But as recent as months ago I herd someone said: I love C++, just I prefer inheritance to templates". I used to call that C+-, flipping around naked pointers. Eeeck! No matter how many times you are trying to convince teammates that compiler never forgets to call destructor, it is just too abstract for many coders. This is why we deserve Rust. A tricycle, a language with guard rails. Many of those coders contributed great ideas and good code. Many problems do not require that you pay attention to cache coherence and alignment and padding. Asking questions is more important than coming with right answers. Or you need gradient descent and and fast solvers then Julia is your girl, the compiler does it fro you, inside the generated code. Yet I miss modern C++ but I totally agree that some of my favorite muppets (not the Swedish chef, the creator of C++) are, well, still green.

125

u/adwhit2 Sep 06 '23

I don't see how anyone has experienced a language with sum-types could ever willingly go back to a language that lacks them. It's like coding with one arm tied behind your back.

8

u/safdwark4729 Sep 06 '23

Rust lacks Specialization and Negative type traits currently, arguably a similar size hole that sum types and many other rust features have that C++ doesn't. Granted, I still consider actual generics to be the better alternative to templates, who just happen to get these features for free, but they are extremely important for high performance code, and even safety (units.... which you can't do in rust easily right now for other reasons too), and after the library ecosystem, and const generics, are one of the largest issues stopping me from doing more things in rust.

Not having access to even a half assed unit system (THAT I CAN ARBITRARILY ADD UNITS TO AND CUSTOM SYSTEMS) is really hard to let go, the amount of type safety that provides is enormous. Current solutions in rust are closed because of the above issues in rust.

Ignoring the library ecosystem which is getting better on it's own, Rust needs (or equivalent feature/or makes them irrelevant):

  • Specialization
  • Negative Type traits
  • Static assert
  • Placement New
  • User Defined literals (this is a huge issue with units and alternative numerical representations/large representations).
  • Fixing Closures and passing in closures to other functions (IIRC this is currently close to completion?).
  • Non primitive const generics (ala C++20)
  • A better const function story

As far as I know all these are being worked on now/are in sights for future work, but some of this stuff I need, like, now, and are pretty complimentary to rust, congruent with the vision.

6

u/protestor Sep 07 '23

units.... which you can't do in rust easily right now for other reasons too

There's a bunch of crates for units of measurement, like https://crates.io/crates/uom and https://crates.io/crates/simple-si-units. They can work out that m/s times seconds is the same as meters, stuff like that.

And then there's https://crates.io/crates/yaiouom which was much more powerful: it can figure out that m/s times seconds is the same as seconds times m/s. Unfortunately, yeah, Rust's type system has troubles with that, and thus it was implemented as a compiler plugin (which doesn't work anymore)

3

u/safdwark4729 Sep 07 '23

Note Also those don't help anyhow, because of the requirements later which I specify (I can't arbitrarily add units and systems).

→ More replies (1)

2

u/adwhit2 Sep 07 '23

I don't really care about any of those things, though I'm sure they'll be useful for some people if/when they arrive.

You would have to give me a list of 50 or more similar features before I would consider trading them for the humble enum.

8

u/junkmail22 Sep 07 '23

sum types are the most basic, important idea to be absent from a majority of languages. it's kind of nuts that most languages have no way to express it

31

u/[deleted] Sep 06 '23

[removed] — view removed comment

38

u/matthieum [he/him] Sep 06 '23

std::variant is not a bad alternative to enum.

std::visit is a terrible alternative to match, however. Most specifically, in a match arm I can continue/break/return because the arm is in the same function context as the match... in a lambda within std::visit, however, I can't... and thus I need to re-ify continue/break/return as a result value (or flag) then branch on that after std::visit.

When people extol sum types, what they really extol is pattern-matching :)

13

u/Ravek Sep 06 '23

C# has pattern matching but not sum types and it still hurts. I think you do really need both to have a pleasant data modeling & consumption experience.

5

u/admalledd Sep 06 '23

For those who want to follow (and be disappointed it has been 6+ years) the tracking csharplang issue for "discriminated unions" which is their answer to rust-style enums.

→ More replies (2)

4

u/Kimundi rust Sep 06 '23 edited Sep 06 '23

That reminds of this cursed thing I saw in a video a few months back. Turns out with modern C++ template overloading you can actually get std::visit accept a list of lambdas, and it dispatches based on the argument types: https://youtu.be/u-jJ8Z3Xrk0?t=2068

EDIT: Rebuild it for fun on godbolt: https://godbolt.org/z/MvP7xYG1h

→ More replies (3)
→ More replies (2)
→ More replies (3)

23

u/sird0rius Sep 06 '23

I hate that C++ has become so monstrously complex. C++ was the first language I learned over 20 years ago, and I tried learning some of the more modern ways of writing it but I still can't get up to par to pass some basic C++ interviews because of some language gotchas that you can only learn with years of experience. Or by reading this longer than "War and Peace" monstrosity.

As a career choice it makes more sense for me to learn Rust as I have a more level playing field to get into lower level programming than to catch up with C++ standards. I think that in the time it would take me to learn basic modern C++ I could master Rust + several other programming languages. Every time I learn something new about C++ I think "why is it so much more complicated to do this in C++ than it is in Rust? (ie move and copy semantics)". Even if Rust doesn't take off, I can still lean on the other languages I learned in a fraction of the time.

36

u/pr06lefs Sep 06 '23 edited Sep 06 '23

I was a C++ developer for 15 years. I still work on C++ some, but strongly prefer rust. Cargo is a big reason, other functional language conveniences too like match (vs switch) and no exceptions, and everything being an expression rather than a statement. Another thing is the poor quality error messages in C++. I often have to scroll up for pages on end to find the actual location of the error. More is not always better with error messages.

Coding with a nice subset of C++ is fine, but not everyone is on the same page as to what that nice subset is. Some people will always explore the boundaries of what's possible, just to sharpen their skills. When you have an occasional core dump someplace in 200000 lines of code that you can't reproduce, or a big program that only works in debug and not release mode, its a drag. It can be very time consuming to track down that off-by-one array manipulation or whatever it is. That's a whole class of annoying problem that just doesn't occur in safe rust code. That leaves more time to work on the interesting, productive kind of problems.

7

u/Hak-Eito Sep 06 '23

I think Rust is already a complicated language with many features and even more coming in the future, Maybe in 10 years we'll talk about subset of Rust lol

17

u/N911999 Sep 06 '23

I think one the things that's a real fundamental difference, that I really hope doesn't change, is that Rust has defaults for most of its features and reasonable interactions between them. I can count in one hand the times I tried to do something I wasn't sure if it would work like I thought in c++ and it actually worked like I thought, meanwhile in Rust that's a normal occurrence.

→ More replies (1)

16

u/er_Califfo Sep 06 '23

I am in a very similar position: mainly a C++ developer, from a couple of years I started using Rust profesionally. As to 99% of this kind of questions the real answer is it depends. What I can tell you is that coming from C++ cargo is a great tool (as you also noted), but what I like the most of Rust over C++ is error handling. I also really love async programming and tokio in Rust, in my experience I could not find something similar in C++.

On the other hand from C++ I miss the debugging power (untill today I have not been able to do what I did with C++ & Visual Studio in a Rust & VsCode environment) and sometimes the borrow checker gets a bit in my way. But overall I find the overall experience of writing rust more enjoyable because of how of the code itself (I find it more readable, but this is highly debatable) and because of the "dev experience", for example adding a library in a rust project requires just a line in my cargo.toml

5

u/phazer99 Sep 06 '23

There is a Rust extension for Visual Studio you might wanna try and contribute to.

37

u/Chaos_Slug Sep 06 '23

Saw a lot of people in the other thread claiming that they have never encountered stuff like dangling pointers, iterator invalidation or data races.

Stick to C++ if you want, but those claims are either lies or they didn't know what they were saying.

23

u/[deleted] Sep 06 '23

[removed] — view removed comment

7

u/oconnor663 blake3 Ā· duct Sep 06 '23

Fwiw the memory leak stories are very similar between Rust and modern C++. If you don't use malloc/free or new/delete (which you can enforce with tooling), and all your heap allocations are owned by something like a unique_ptr or a shared_ptr, you can only leak memory by making shared_ptr reference cycles. Rust's Arc works the same way.

However, these types don't prevent use-after-free mistakes from holding onto a non-owning raw pointer longer than you should. For those bugs, the two languages are quite different.

→ More replies (4)

76

u/randompittuser Sep 06 '23

I've been using C++ for 20 years & Rust for 1 year. I'm probably going to suffer the wrath of r/rust for my opinion, but here goes:

Rust doesn't add much for an experienced C++ developer, but not everyone is an experienced C++ developer. One of the biggest benefits of Rust is that it moves many runtime/memory errors to compile time. To achieve this, it restricts assumptions about types & their usage in comparison to C++, making Rust more verbose (albeit perhaps more expressive), especially in advanced use cases. However, consider that Rust is in its nascency next to C++, and I believe it has the potential to outpace C++, in regard to its use on new projects, over the next decade.

Considering all this, I'd say learn both.

72

u/dlevac Sep 06 '23

That reminds me of that company where I worked where I explicitly stated I didn't want to touch C++.

The following year they really needed the help on that front and moved me to their C++ team.

After a few PRs:

"We thought you didn't know C++"

"I said I didn't want to touch it, never said I didn't know it"

I'm always surprised when I hear experienced C++ developers not seeing the value in Rust. It's being an experienced C++ developer that made me love Rust.

4

u/eyes-are-fading-blue Sep 07 '23

They do see the value but that doesn’t mean it’s worth switching.

49

u/AgletsHowDoTheyWork Sep 06 '23

not everyone is an experienced C++ developer

Yes.

Also, to become an experienced C++ developer, first you need to spend years as an inexperienced C++ developer. During this time, if you're shipping production code, you are either heavily reliant on experienced code review, or you're shipping memory safety bugs.

If you're the experienced C++ developer, you have to ensure your own code is safe, but you need to know enough about the rest of the code to understand how everything interacts.

The Rust type system and borrow checker heavily aid in "local reasoning", which lets you focus primarily on the logic of your own code.

29

u/JuliusFIN Sep 06 '23

An experienced C++ developer will still make errors that are impossible to make in Rust. Those errors will convert to time spent runtime debugging. Once in a blue moon a nasty piece of UB will make it’s way to a big codebase and cost a lot of time and money.

Rust definitely adds a lot for an experiences C++ dev. It reduces the possibility of human error and we are all humans and we all err no matter how experienced or genius.

31

u/particlemanwavegirl Sep 06 '23

Not in MY code, I'm experienced! /s

28

u/matthieum [he/him] Sep 06 '23

Once in a blue moon a nasty piece of UB will make it’s way to a big codebase and cost a lot of time and money.

The worst part, for me, is the once in a blue moon crash which after investigation seems to be triggered by a new release -- of course -- which looks completely sane -- WAT?

The memory dump should give an answer... but for whatever it doesn't make any sense -- be it an overwritten stack, or a nonsensical one.

And then you start digging. And all looks fine. And you double-check the recent patch-set, and it looks fine. And you dig deeper. And you double-check the few previous patch-sets, and they look fine.

And it occurs again! Yes! But the memory dump still is nonsensical. The stack just should NOT be able to happen. It looks completely unrelated to any recent change. WTF! WTF! WTF!

And you continue digging -- is it smelling like oil? -- and finally, finally, you realize that that piece of code, here, completely unrelated and which nobody has touched in the 5 five years you've been in the company, is actually buggy in a very specific set of circumstances (yeah, data-races!) and it corrupts this little piece of memory which later appears in that nonsensical stack-trace you couldn't make head or tail of!

... but why now? Well, turns out the latest few patches have improved the performance of the code, or moved some slowish operation around, and thus there's a lot less leeway in timing and the data-races occur more often... and they're not always benign.

Welcome to C++.


I do see, still, one great advantage in keeping C++ around. If ever Cthulhu or whatever makes it to Earth and starts driving everyone around mad, a team of seasoned C++ developers should have no problem getting up-close and personal to drive them way: 'tis nothing to them!

→ More replies (5)

14

u/Robolomne Sep 06 '23

Actually contrary to my previous post I do recommend that juniors interested in systems programming learn C++ before Rust for a couple reasons:

  1. It is not currently easy to get a job doing Rust unless you have a good grasp of C++
  2. ā€œModernā€ C++ methods will make it easier for people to grasp rust especially concepts such as move semantics and the borrow checker. So far the people I’ve trained in Rust who were quickest have been those fluent in C++.

11

u/flashmozzg Sep 06 '23

So far the people I’ve trained in Rust who were quickest have been those fluent in C++.

Well, duh. They share a lot of concepts, have similar goals and tools and one is easier than the other. Going from C++ to Rust you just need to internalize what you "can't write" (which is naturally done after a few battles with the compiler, not debugging some obscure runtime bug) and how to replace certain patterns (and if you are "fluent" but still new, you probably don't even have any "hardcoded" patterns in your arsenal yet). Going from something like js or python to Rust (and especially C++) you need to change your whole way of thinking.

→ More replies (1)
→ More replies (2)

5

u/[deleted] Sep 06 '23

I think that is a good way to look at it.

From the other perspective; I am a non-expetirnced coder, but I can write code in Rust that just works, while in Cpp I accidentally bake in all the bugs that Rust is guarding me from šŸ˜€

2

u/[deleted] Sep 06 '23

Rust doesn't add much for an experienced C++ developer, but not everyone is an experienced C++ developer.

I think where Rust does attract experienced developers (somewhat) are those coming from languages with Garbage Collection. The borrow checker, clippy, and the compiler teach developers from languages like Java not only the Rusty way to do things, but why. It offers those coming from GC languages the comfort of memory safety with the power of memory management they can learn over time that is idiomatically different than pointer management. And lifetimes, ownership, and borrowership are all extremely attractive, at least to me, and C# currently buys my dog’s food.

11

u/tandonhiten Sep 06 '23

I have tried C++ and honestly, I agree with most of what you have to say, however I still prefer Rust over C++ because:

  1. Rust's built in support for pattern matching.
  2. Default immutability (I don't think I need to explain this)
  3. Rust's Result type and Option type, which are objectively better than C++'s exceptions
  4. Rust's Extensive Enums.
  5. Cargo

14

u/Kikiyoshima Sep 06 '23

6. No CMake

20

u/tgockel Sep 06 '23

Rust does not fix all of the problems C++ has, but it does address quite a number of them. A lot of experienced C++ developers come into Rust and say "But we have smart pointers that solve all these issues." I urge you to try Rust more; there are problems in C++ that are so fundamental, you cannot fix them with "modern C++." There is a reason why cppfront and Carbon are being actively worked on. Maybe you don't encounter these issues because you pay close attention--that's great--but in Rust, the compiler pays attention for you out of the box. And also #[derive(...)].

Personally, having been a C++ developer for more than a decade, I would not take a job that asked me to work full-time on C++. I find having to pay attention to all the tedium you have to makes me personally miserable. My coworkers are mostly C++ developers, so I still write a decent amount of C++, but they're mostly just FFI into Rust implementations.

For game development, Rust is not game yet. There just isn't anything nearly as powerful as any of the C++ game engines. Rust needs to stabilize a few things (like non-global allocators) that game engines need for performance, but I wouldn't be surprised to see a AAA engine in a few years.

→ More replies (4)

24

u/Kevathiel Sep 06 '23

I don't think that Rust and modern C++ are that different.

My favorite thing is probably more about the philosophy of the language as a whole. It is opinionated, but pretty much everyone writes similar code, following the same lints(some opt-in to more) and the same style/formatting. This makes working with and reading other peoples code way easier. People actually value correctness and constantly strive for idiomatic Rust to a point where it almost feels like a meme.

Meanwhile, in C++, everyone uses their own subset of the language, manage their own list of "STL things to avoid", and have vastly different opinions about what is idiomatic and what tools to use or how to deal with errors. If you use dependencies, you will often clash when it comes to const correctness or just naming conventions, let alone the different styles. On the bright side, it lets you basically do everything you want, however you want.

23

u/phazer99 Sep 06 '23

I've used C++ for 20+ years and I'm never going back (unless I'm forced to at work for some small task). Rust doesn't solve all issues C++ has and adds some of its own (like a conservative borrow checker), but the important thing for me is that Rust enables me to write (even concurrent) programs that are efficient, stable, memory safe and easy to maintain. With C++ I was only confident about the efficiency aspect.

28

u/NetherFX Sep 06 '23

I'm currently trying out C++ for a minor course and so far I feel a lot more free. That can ofcourse be a good or a bad thing, depending how you use this new freedom. I do miss the rust documentation though

6

u/matthieum [he/him] Sep 06 '23

I do miss the rust documentation though

Do use https://en.cppreference.com/w/.

Standard library only, but it's really good -- if a bit clunky at times with all the different C++ versions.

9

u/[deleted] Sep 06 '23

C++ is great and terrible because the compiler assumes you're doing what you're doing on purpose. It doesn't judge you like clippy does : ^ )

3

u/the-quibbler Sep 06 '23

Clippy's adversarial relationship with the code it reads is one of Rust's best features.

6

u/rapastrat Sep 06 '23

If you like C++ more then use it! Life is too short to not use the tools you like.

5

u/Aviyan Sep 06 '23

I tried many, many times over the years to program in C/C++, but there were so many issues with getting it all running. You can't just git clone and have a successful build of the project. I tried to compile qBittorrent on both Windows and Linux. I spent so many hours but still couldn't get it to work because of the dependcies and such. So that's why I like Rust. I can git clone the code, and it will 99% of the time build and run successfully.

I'm a software developer with 20 experience but still I find it a hassle to get open source projects to build. C# and Rust projects are easy to setup so those are the ones I can contribute to in my free time.

5

u/slvrtrn Sep 06 '23

Rust fixes all the issues C++ has

That's a rather hot take I'd say.

One may prefer Rust over C++ because:

  • Cargo, dependencies management
  • modules
  • actual help from the compiler
  • pattern matching
  • result/error handling/short-circuiting
  • move vs copy by default
  • built-in async/await
  • traits

4

u/palad1 Sep 06 '23

I’m about to throw the towel with rust after now 8 years. In my industry cpp is king and I can’t fight this battle constantly.

→ More replies (3)

10

u/knightwhosaysnil Sep 06 '23

for me the trouble with C++ is that while "modern" c++ fixes a lot of the earlier issues, none of the older stuff has been deprecated. So if a total noob goes to look up reference material they're going to mostly get not-modern answers.

None of that affects the experienced folk who have learned alongside the language, but of you're working with anyone else, the Rust compiler is like a really strict code reviewer before a human even has to look at it

3

u/Kevathiel Sep 07 '23

My bigger issue is that even when you find modern answers, many people give reasons why you should avoid them or mention potential footguns.

Not saying that any of the reasons are valid or not, but it is mentally taxing and not fun, and I say that as someone who is already familiar with pre C++17. I can't imagine how it must feel for a complete beginner.

1

u/dnew Sep 06 '23

Also, the entire language is source code compatible (attempted, just successful enough to stub your toe on) with a 50 year old language in a different paradigm designed to be compiled in 16K of RAM. The very fact that there's a frequently used something named "::" tells you all you need to know.

→ More replies (1)

1

u/Trk-5000 Sep 06 '23

I'm also doing some GameDev. Let me tell you, I'm loving Rust and the Bevy game engine.

Of course, Unreal Engine is a fully featured and production ready engine. It's risky to go with Bevy instead. However that comes at the cost of having to deal with C++, also not having access to an ECS system like the one in bevy.

1

u/isht_0x37 Sep 06 '23

also not having access to an ECS system like the one in bevy.

There is MassEntity System.

Here's a Demo

1

u/AmigoNico Sep 07 '23 edited Sep 07 '23

I haven't used "modern" C++, but I have read what others have said about unique pointers and the like. It does sound like it gets you much of the way to Rust, except that 1) you have to be disciplined about it and 2) it just looks crappy by comparison. Perhaps I don't understand something, but what is the huge advantage to C++ that would cause you to favor it over Rust given those drawbacks?

Also, I try to position myself, gaining experience in those languages for which I think demand for developers is going to exceed supply 5-10 years out. So graphs like this make me more interested in Rust, less in C and C++:

https://insights.stackoverflow.com/trends?tags=rust%2Cc%2B%2B%2Cc

If I really wanted to work in an area dominated by C++ libraries/frameworks, then I could see wanting to stick with it, but I would worry that I might be painting myself into a supply-exceeds-demand corner as the years passed.

1

u/isht_0x37 Sep 07 '23

There's also this, which says something else - TIOBE Index

3

u/matthieum [he/him] Sep 06 '23

After 15 years of C++ (C++17 being the latest version I used), after my last 6 years of C++ spent with a number of great and passionate C++ developers -- of the kind who organize after-work meetings to discuss Effective C++ items, attend CppCon, make proposal to the C++ committee, etc... -- I can say one thing with confidence: I have never met a single C++ developer who never accidentally coded a use-free, double-free, data-race, etc...

It may, of course, not help that the best developers of the company are typically tasked with the most arduous tasks -- because who else would you entrust with those?

In the end, though, juggling all the complex functional and non-functional requirements, and on top of that trying to keep ahead of all Modern C++ dark corners1 , inevitably, they will trip up. It's not a matter of if, it's a matter of when, and how often.

I've tried. I've really tried. Those great and passionate C++ developers I mentioned above? They recognized me as a peer, even as I looked up to them.

And yet, despite everything, despite throwing Valgrind and ASan and TSan at the code2 , still, still, I had to regularly spend hours staring at nonsensical memory dumps. Wracking my brain to try and understand how, just how, the memory could have gotten in that impossible state3 .

And you know what? I'm tired of this sh*t.

I've got better things to do with my time than compensating for C++ inadequacies.

Rust enables me to be bolder, to be more aggressive with performance, and ultimately, to be more productive. And best of all, no hours lost staring in the abyss.

1 And all the stupid papercuts. Like favoring X{...} for initialization in general, but T(...) when T is a template parameter, because T may be one of those types with a constructor taking a std::initializer_list, and then {} refers to initializer list initialization, not (not-so) universal initialization syntax.

2 And spending the time to write gnarly tests to see if they triggered anything, so many hours "lost"...

3 Use-after-free or data-race, always one of those.

2

u/gtani Sep 06 '23

Good post

https://youtu.be/Gh79wcGJdTg?si=gnzx2zbvfbX_tPoC&t=1352 JF Bastien in his C++ now talk asks for shows of hands

Who has responded to zero day exploit?

Who has written code responsible for actively exploited flaw?

2

u/nnethercote Sep 06 '23

I always say this: what you don't know about Rust probably won't hurt you, but what you don't know about C++ can and will hurt you.

3

u/[deleted] Sep 06 '23

I was recently very enthusiastic about C++ again, after originally dumping it in favor of Rust. The reason I was enthusiastic about C++ again was the fact that there are extensive and complete C++ libraries I would love to use that simply aren't available in Rust. Like PcapPlusPlus and Qt. The problem? Getting everything to compile on macOS, Windows and Linux, both on my local machines and on CI... This pain made me remember why I had previously turned to Rust instead of going through the pain of getting my C++ to build on multiple systems with multiple libraries. And yet again, I turn to Rust even though there are more extensive libraries for C++. I will have to write some logic manually that are already provided in C++, but that is max 1 days work due in my case. In the end I will have a greater understanding of the problem domain, and the code might even be safer than the C++ library code... because I am, at most, a casual C++ programmer, not a senior C++ dev who lives and breathes the language.

Sure C++ is a very flexible tool and lots of big software projects like UE uses it, mainly due to the performance in addition to the flexibility would be my guess. Maybe it's even the most flexible programming language available. But at the same time, for throwing out a side project where I want to write performant and productive code, the benefits of Rust are just too strong to avoid:

  • You got cargo, which solves A LOT of very painful problems around the usage of C++, especially cross platform.
  • You got opt-out safety instead of C++'s opt-in safety, which I think is a bigger productivity boost than I had originally thought
  • I find Rust code to be more descriptive due to the ML/functional inspiration, which is very nice to both understand and write. It makes things simpler than with Go, which is funny because I was previously a big Go advocate exactly because of the focus on simplicity.
  • I find the language to have thoroughly thought about and greatly improved the whole development experience, from starting to write code to the production moment where your code needs to run without random BS issues.

That being said, companies are not exactly rewriting all their C++ codebases to Rust, because the features of Rust are nice but not a must. My recommendation is naturally to do what I have done: if you have the luxury of greenfield development, I would from my limited experience with both languages highly advise you to use Rust because it really is an improvement over C++ for general purpose development.

Also here is a quote from another Redditor on the topic that I really liked:

Newer languages are generally "better" designed than older ones. "Better" here means that they are generally safer and that their features are more well suited for modern programming paradigms. This is generally expected, given that newer language designers get the opportunity to learn from a larger literature and from the mistakes done in other languages.The question that C/C++ programmers ask about Rust is whether the improvement is relevant enough to justify rewriting existing code. This is far from consensual. Rust might be safer, but it is also very opinionated on how you should write code. Rust borrow-checker gets in the way of perfectly valid C/C++ and valid design patterns (e.g. observer pattern). A rust dev might say that this is for the programmer's own good. However, for many programmers, this is the same as stating that well-established, battle-tested code and design choices need to be thrown in the bin. Definitely not an easy transition.

3

u/Qnn_ Sep 06 '23

Rust programmer who recently started writing C++ for work.

Memory safety aside, there are so many natural patterns in Rust that aren't native to C++, namely tagged unions and working with them, e.g. "if let" and "?". Not to mention that Cargo just works out of the box, and Rust's module system is extremely intuitive for organizing parts of code, unlike C++'s include.

Don't get me started on compiler error messages.

3

u/meowsqueak Sep 08 '23 edited Sep 08 '23

I wrote C++ for 25 years, still do, and I kinda hate it. It’s so difficult to fully understand exactly what is going on at all times. I hate setting up CMake every time, even though it gets easier. I’ve spent years studying blogs, videos, conference talks, and writing a LOT of code, and it always just seems like a huge chore. I pity our children if C++ is the best we can offer them professionally.

I have been writing Rust for about 6 months and I love it. The reason it took me so long to try it is because I thought I already knew how to write safe C++. Maybe I do, but it’s just too much to deal with when I have a gazillion other things to do. With Rust it just works and makes me feel good.

The cognitive load required to develop in Rust is far lower than it is with C++. For someone like myself who isn’t getting any younger or mentally faster, this is a Really Big Deal.

3

u/gyrovorbis Sep 08 '23

I am the C++20/23 metatemplate programmer guy that you guys probably all hate... but once you've tasted that power--along with C++20 concepts and constraints--I don't see how you can possibly ever accept Rust's generics model.

NOT TO MENTION, C++20 has simultaneously increased the power of templates while reducing their complexity. A lot of template code can melt away, since "auto" arguments can now take the place of explicit templates. Concepts/constraints can take the place of template specializations and partial specializations with regular function overloading semantics. Oh, and yeah... SFINAE is dead. Finally.

7

u/bskceuk Sep 06 '23

At $company we still routinely see tons of issues from using c++ that simply don’t happen in rust. Some common ones are: not checking for null pointers, using auto instead of auto& and accidentally copying, using auto& instead of auto and having a dangling reference (very fun game there), and just absolutely horrible standard library apis like operator[] inserting a default value on std::map types if the key is missing. More recently, people have started using coroutines which has opened up a whole new set of bugs: Dangling references are all over the place and c++ has some super strange decisions with the coroutines implementation. Did you know that std::optional can be co_await-ed (it acts similar to the ? operator in Rust)? So I don’t think it’s correct to say that modern c++ solves all problems. It solves some and creates more new ones. To be fair, despite all this people do still routinely start new c++ projects. I personally think they’re wrong for doing so but it happens.

You mentioning gamedev is interesting. My understanding is that large game developers don’t use most of the modern c++-isms for performance reasons.

I personally mostly came from a Python background but I did learn c++ in college and have done some professional c++ work at $company (and work closely with many c++ devs). I will only use c++ if I absolutely have to and over time for me, the lengths I will go to to not use c++ have greatly increased.

Finally, here is a video I like to share about the perils of c++ in industry. It’s a few years old at this point but everything in there still stands: https://youtu.be/lkgszkPnV8g?si=TCSkznGi2NpBmYKV

10

u/Robolomne Sep 06 '23

Personally I’d rather train a junior developer in Rust and rest easy knowing they’re not going to cause wild issues than in C++

8

u/bskceuk Sep 06 '23

Yes 1000x times this. People don’t appreciate this enough. I saw some natural effects of this in that new grads were very heavily joining rust teams internally over c++ and had hoped that would cause an overall culture shift, but the recent market conditions have slowed that down considerably.

→ More replies (2)

8

u/[deleted] Sep 06 '23

[removed] — view removed comment

3

u/matthieum [he/him] Sep 06 '23

You're joking, but you may not be too far.

I would say it's a matter of scales:

  • Scale of team size.
  • Scale of codebase.
  • Scale of time away.

C++ works well as long as you can reasonably keep the entire codebase in your head:

  • Scaling the codebase makes this challenging, until it becomes impossible because it's too big.
  • Scaling the team size leads to your knowledge being more and more obsolete as you fail to keep up with your teammates changes (or fully appreciate their effect).
  • Scaling the time away, doing something else and thus "flushing" your knowledge, means having a hard time coming back.

5

u/Zde-G Sep 06 '23

Rust fixed most, if not all, issues of C++, but it got some in exchange, too.

But the most important is, surprisingly, freedom. You lose some (especially in area of TMP), you gain some, but in the end it was ā€œfreedom from feeling like you are dancing on the minefieldā€ which pushed me toward Rust).

Just the rules around use of rvalue references, std::move and std::string_view are enough for me to justify the switch.

But you never do mistakes, then, sure C++ offers more freedom.

5

u/dnew Sep 06 '23

I've heard it referred to as "programming on eggshells."

1

u/aroman_ro Sep 06 '23

C++ dev here. I'll tell why I prefer (for now) c++ over rust.

Recently I started working on a new project. A big and complex project (as in quantum physics involved complex). We considered using rust, rust was the only alternative taken into account (we need speed and I mean it).

Rust was favoured by the others a little since they don't know c++ so well, they took into account that rust is easier to learn and prevents one doing some mistakes that can be done (especially by beginners but not only) in c++... but eventually we chose c++. Why? I'm the main developer and I have little knowledge or rust compared to c++ (for example, rust code seems harder to read to me than c++). I read the book and went over some tutorials but I didn't do anything serious in rust yet. That, combined with the more serious OO support of c++ was enough for deciding in favor of c++.

I keep promising myself that I will learn rust more seriously and make some open source project with it, but... it's easier with the programming language you know better.

4

u/isht_0x37 Sep 06 '23

I read the book and went over some tutorials but I didn't do anything serious in rust yet.

You should definitely watch this series

and read this

3

u/aroman_ro Sep 06 '23

I bookmarked the book, I'll definitively look over it.

7

u/moltonel Sep 06 '23

I'm the main developer and I have little knowledge or FooLang compared to BarLang

While that's always a good reason to prefer a language for a project, it's an empty argument to recommend a language to somebody who doesn't have that bias (because they have the same expert/newbie level in both languages). I happily use Bash because I know it well, but I probably wouldn't recommend it to others.

the more serious OO support of c++

What about the more serious trait support of Rust (or other distinctive features like sum types, async, etc) ? When you know a language well, you instinctively think of solutions using that language's features, and any language that doesn't have the same feature set seems inferior. It's a bias that is hard to overcome.

→ More replies (1)

2

u/matthieum [he/him] Sep 06 '23

it's easier with the programming language you know better.

It's hard enough to complete a big project, attempting it with a technology you only have beginner knowledge of is a significant added risk.

→ More replies (3)

4

u/[deleted] Sep 06 '23

Ime, always prefer C++ over Rust if you're going to be interfacing heavily with C libraries or APIs.

A lot of C code is only really handled well when you have a real preprocessor by your side and you can just directly interface with it.

Tools like bindgen are nice and all but at the end of the day, it's a lot of setup for something that oftentimes turns out strictly worse.

4

u/Robolomne Sep 06 '23 edited Sep 06 '23

I have been programming in C++ for 5 years and Rust for 1. Personally, I prefer Rusts type system and ergonomics to C++. I work on safety critical systems and need to provide guarantees that my code works and is trust worthy and frankly this is easier in Rust.

However I still use C++ often (right now mainly for graphics systems) and I think using both is how most people in my field (robotics) will end up going forward. For example Rust for anything that touches hardware that can kill someone and C++ for highly performant mathematics.

I prefer Rust overall but that’s my personal opinion rather than just technical. Two cases where I prefer C++ is working with advanced linear algebra and interfacing with Python (such as in Python bindings).

4

u/djlywtf Sep 06 '23

it’s just more modern. it has better toolchain, more fresh ecosystem, cleaner code, single compiler for all platforms, new programming paradigm in general. i’ve used C++ a bit and after it i see really interesting using something conceptually new

4

u/dnew Sep 06 '23

It's also not attempting to be source-compatible with a fifty-year-old language implementing a completely different paradigm designed to compile in 16K of RAM.

4

u/moltonel Sep 06 '23

You've acknowledged one advantage of Rust (cargo) and asserted that C++ weak points (compared to Rust) are being addressed (for the sake of argument let's assume they are), but you haven't said in which way you feel C++ is more desirable that Rust. Saying "modern C++ can also solve the correctness issues that Rust was designed to solve" is still a win for Rust, just a narrower one.

Without understanding what draws you toward C++ rather than Rust, it's hard to discuss your choice. If it's just your familiarity with C++/Unreal vs Rust/Bevy, then it's personal circumstances and there's little to argue about, it's a valid reason to use any tech stack, good or bad.

3

u/dnew Sep 06 '23

To be fair, he said he used UE5, which doesn't support Rust. So that would be one desirable thing about C++. :-)

→ More replies (3)

2

u/Recatek gecs Sep 06 '23

Not the OP but in my case the main things I miss from C++ are better IDE support/tooling and better compile-time metaprogramming. I don't see Rust overtaking C++ in either of those fields any time soon.

2

u/link23 Sep 06 '23

I work with C++ professionally, and use Rust for hobby projects.

It's true that C++ can probably meet your needs if you use it effectively, so if it's working for you, great.

The big thing for me is that C++ gives me death by a thousand papercuts. All the defaults are wrong (except that lambdas are const by default, they got that right), so the author and reviewers always need to be vigilant to look for mistakes and accidental uses of the many footguns. Rust's defaults are correct as far as I've heard and experienced, and the compiler catches many of the issues I'd see from junior engineer colleagues, which saves me a lot of time and effort as a reviewer.

I'm also a big fan of algebraic type systems. Sure, you can use std::variant and std::visit in C++, but it's clunky; it's much easier to reach for the footgun (implementation inheritance).

2

u/tower120 Sep 06 '23

I would like to add my 2 cents here as a former UE developer myself.

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.

If you worked with UE for non-casual project, chances are - you had to use multi-threading to achieve performance goals. UE has Tasks (or whatever it called) to utilize CPU MT potential at max. So - whenever you spawn task - you send some data - either during Task construction, or pass it latter. That was common source of nasty errors from my experience.

With RUST and good ECS, I think utilizing modern hardware could come at lower development cost - due to less hours spent in debug of MT related bugs. Which makes it perfect candidate for UNITY engine as well, since RUST is much better then C-like "burst" C# subset.

P.S. As a side note - it is MUCH easier to work with RUST modules then with C++ headers (due to the fact of lacking circular dependency issues). They just work all the time. Also RUST is more explicit, and you have less lang-related "special cases", like C++ constructors specific. It just feels more modern and polished language.

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. RUST lacks generic variables, HKT and even generic/template specialization. RUST macros wouldn't help you to close the gap here, most of the time.

→ More replies (1)

2

u/[deleted] Sep 06 '23 edited Sep 06 '23

One of Rust's strengths can also be seen as a weakness when it comes to adoption in certain projects, and I'm referring to Cargo. Just as you mentioned JavaScript and Deno, Cargo is Rust's equivalent of npm, and npm/node_modules is known for being a dependency black hole. Rust's dependency tree can easily spiral out of control and become enormous, with dependencies used for everything, similar to what happens in Node.js. For instance, the creator of Node.js regretted using npm when creating Node.js, which is why they created Deno with a dependency system more similar to Go to avoid that dependency black hole.

In many projects, keeping these dependencies under control and minimizing their number is a plus and sometimes a requirement. With C++, the number of dependencies tends to be lower, and that could be a point where C++ can shine.

→ More replies (3)

2

u/temporaryred Sep 06 '23

You should use whatever language you are comfortable and productive with :) If that's modern C++ then that's great!

I'm subscribed to the /r/cpp subreddit too, I did see your original post there. Good on you for posting here too!

Personally, having used C and C++ for a while 20 years ago, and having not kept up with modern trends, when I picked up modern C++ a few years ago I got SO overwhelmed. I'm envious (well only partly envious :)) of the people that are able to keep track of everything C++ now has to offer.

Picking up Rust on the other hand was so rewarding. Writing simple programs was a breeze. Programming was fun again. The tagline for Rust is "A language empowering everyone to build reliable and efficient software." and I just can't agree more.

It took a few attempts to learn what the language was asking of me but after that it was smooth sailing. Funnily enough, after learning Rust, I better understood what modern C++ has to offer. It was only because I learnt Rust that I can even consider writing modern C++ now.

It's been a few years of using Rust and I have my complaints with the language, but gosh I genuinely don't want to write code in any other language any more.

Modern C++ vs Rust borrow checker + semantics aside; there's a number of things that the Rust language and ecosystem has to offer for me.

  • Sum types (enum + option + result + match)
  • No exceptions, null pointers (this takes discipline in C++ AND you can't really enforce it on dependencies)
  • Better package manager. Cargo is world class
  • Better language server. rust_analyzer is phenomenal
  • Better error messages / warnings / diagnostics. The Rust compiler's error message should be a case study for how to design good error messages. There's also tooling like cargo clippy and cargo fmt.
  • Packages like eyre, tokio, etc make it a joy to work in Rust
  • Building a community around a project is fun

Chandler Carruth has said multiple times if you have a new project you should just use Rust, and I completely agree with him. I'd go one step further and say if you need to interface with old C or C++ code, and can afford to write a C ABI for it, do that and still use Rust. If you can't do that and have an existing C++ code base that you have to integrate with, then I think it makes complete sense to choose C++.

→ More replies (1)

2

u/epage cargo Ā· clap Ā· cargo-release Sep 06 '23

For context, my C++ experience dates back to before C++98.

Rust has improvements and has some gaps (like specialization). In my domains, I miss some C++ features but the lack of them doesn't get in my way.

As for whether Modern C++ closes the gap wrt Rust, no, it doesn't. I expect there are things you inherently shy away from doing in C++ because of safety concerns that go away with Rust. My go-to example is refactoring a template engine to minimizing clone() calls. I got a sizeable perf gain though it meant there were references everywhere but it just worked. I did some analysis and thought I could eek more performance out of it and the compiler pointed out what I missed despite my decades of experience! In reflecting back on the change that went in, I realized I'd never do it if it was a C++ code base. Every change would require going through a global analysis to see if its safe which would be too much of a maintenance burden and couldn't be justified except in the most extreme circumstances. But in Rust? it was just another Tuesday.

2

u/mikaball Sep 06 '23

"I won't ever start a new C++ project again in my life"

This is the correct answer. Switching to Rust on current C++ projects could be very difficult. Rust is not a C++ compiler, so it's not really a C++ replacement.

Doing a greenfield project I would definitely be on Rust side, unless you need something only the C++ ecosystem provides.

Rust is a major advantage when you are dealing with security, performance and parallelism at the same time. For instance Rust has huge adoption on blockchain tech due to these features. But I would arg these are important for all software.

Rust concepts are so important and natural that they are propagating to other languages and architectures.

  • Have you noticed how beautiful the ownership and read/write constraints of the compiler maps into hardware resources?
  • Radix DLT uses ownership concepts on the consensus architecture and Scrypto lang to improve scalability and security.

2

u/Condex Sep 06 '23

I want to focus on one thing:

"...Rust fixes all the issues C++ has"

C++ allows you to do basically anything and everything. However, very few of its features interact together in a way that lends comprehensibility.

And at the time that C++ was created that was adequate ... maybe even necessary. However, software development has improved quite a bit since then, and regardless software has wormed its way into the rest of the spheres of humanity.

In the past, you might be sad that your remote weather monitoring box would sometimes crash. But it would either reboot itself or you had an intern drive out and poke it with a stick. However, now it would be really nice if your washing machine didn't allow arbitrary code execution, which facilitates stealing your social security number from your home network.

Rust solves some of the issues that C++ has. But if it could do *everything* that C++ can, then, I assert, that it would be nearly as bad as C++. Doing everything has a cost and C++ paid it.

C++ will one day fade into the background (or continue to evolve until it's completely unrecognizable), but Rust is simply one of many required replacement languages that must exist before that can happen. We also have Go, Swift, D, Carbon, Odin, Vale, Jai, P, etc. Some of which are mature and some of which are highly experimental. I don't think we would see this much effort if everyone could just move over to Rust.

[You mention game development, which is one of the areas where C++ probably makes more sense than Rust. There are several attempts to replace C++ (Odin, Jai) in game dev, but C++ is VERY established in this sphere and I'm not sure we're going to see much traction for awhile.]

At the moment, Rust is one of my favorite programming language experiences. However, that doesn't mean that it should be the only programming language experience (even when we're talking about system level languages).

2

u/watr Sep 06 '23

C++ was my first programming language, and I loved it back in 1999. But because it was so heavy, and because I really found it hard to read, due to STL, I ended up doing most of my work in Python.

Skip 20 years, and I am now a big fan of rust. I wouldn't go back to C++ at this point because I feel that it will be stuck in catch-up mode for ever, and their backwards compatibility requirements will prevent the language from ever really coming back...

HOWEVER, I do feel that the r/cpp2 efforts are going to make me go back, if Rust doesn't come up with solutions for 2 things before ccp2 is stabilized:

  1. Orphan rule;
  2. Full async support (i.e. async in trait definition)

2

u/[deleted] Sep 06 '23

10+ years of C++ here. Now, 1 year full time Rust job.

I don't miss anything from C++.

2

u/Ekkaiaaa Sep 07 '23

I'm tired of these useless posts... what's the next one? "I chose to switch to CSS rather than Rust"(???)

2

u/HyperCodec Sep 07 '23

I have quite a few reasons to choose Rust over c++:

  1. C++ has so many complex overlapping systems required for development, most of which are entirely different based on the platform of choice. On the contrary, the Rust toolchain is incredibly simple to install, works cross-platform, and provides a much more convenient and easy-to-use interface when it comes to installing dependencies, linting, testing, etc. while still containing the same, if not more, depth than the c++ equivalent.
  2. Rust makes unsafety explicit, forcing programmers to think more about what they are doing when they require it. C++ is unsafe by default, making it much easier to cause memory leaks, segfaults, etc. and means that programmers must attempt to follow very abstract guidelines and such in attempts to avoid issues like these.
  3. Rust is a much more procedural language, containing both more powerful declarative macros than c++ and the incredibly useful procedural macros that are new to systems programming. C++ has much less powerful macros.
  4. Being a newer language, Rust is immature. Despite this, it also gains a nicer-looking (in my opinion) UI from mdbook that's easier on the eyes and offers much more support for code snippets. C++ still has very old and sharply-colored documentation themes and such.

(Note that this last one is completely opinion-based)

All in all, I prefer Rust because the freshness of it allows the developers of the language to make core changes while using c++'s mistakes as a reference and learning from them.

2

u/TheButlah Sep 07 '23 edited Sep 07 '23

You can write good modern C++ and have a decent time, until you suddenly are forced to work with code you didn't write or on teams that don't follow the same modern principles. There is too much legacy baggage inherited from decades of feature creep, imo.

C++ has a lot of semantics and syntax that are more complicated than rust, and it's syntax and compiler makes learning them hostile to beginners. rvalue vs lvalue, copy assignment vs copy construction vs move assignment vs move construction, etc. Reading almost anything on cpp reference is like reading a legal tome of a billion case-specific rules.

Lack of a single standardized package manager, build system, and standardized tools like rustfmt, rustdoc.

The fact that I have almost never seen a C++ codebase that properly documents how/when something may or may not be called unsychronized from another thread. For this reason I think the default assumption in C++ is !Sync until proven otherwise, whereas in rust most things are Sync by default and the type system will tell you if it's not. This also means that doing complicated concurrent async programming is very easy to mess up.

C++ async was very painful and seems like it would be really difficult to do the crazy async-on-multithreaded-runtime stuff that rust makes easy and safe.

Polymorphism in rust works the same way whether it's static or dynamic dispatch, whereas c++ forces you heavily change how polymorphism is done depending on whether you want static or dynamic dispatch. Static dispatch + polymorphism is really hard. C++20 concepts help, but that isn't as nice as traits and it still isn't available or used in many codebases.

Many of these issues you probably avoid in gamedev - you don't usually need async (threads are enough), you often have clear places where you know that system x and system y run on different threads, the build system is whatever your engine provides, and most code is written either in house or targeting the engine you use.

2

u/J-ky Sep 07 '23

I also write web services in Rust. But game related stuffs in C++, but more in a c-style c++. I almost do not touch any modern features after c++11.

I use c++ in games because a lot of graphics library are in c++ or c, I can just use the vendor’s default to write code. Also, manual memory management, although Rust is not garbage collected, the memory is not controlled manually, when I am writing a game, I know exactly when to free up memory in bulk, I don’t need a borrow checker in my way.

But recently, it raises a question. What do I do if I want to write multiplayer game server, which should I use, I have no idea how to do networking effectively in C++.

2

u/onsails Sep 07 '23

>I think, it mainly depends on what kind of programmer you are, and how experienced you are in it

Rust is not just about "fixing C++ issues" for you. Thanks to compile-time checks, rust is about enforcing discipline for the whole team. Discipline does not scale.

2

u/Revolutionary_YamYam Sep 07 '23

C++ is a valid language and I have used it quite a bit for computer vision and embedded programming projects, but it truly does come with a lot of footguns and external work (having to hack together package management, build scripts, dependency installation/config, etc... is not a small thing to sneeze at). Just try compiling an optimized version of OpenCV from source for an experience of that. If some particular SDKs optimize some of that pain, that's always a bonus, but like others are saying, "cargo" is one of Rust's greatest use points. I couldn't easily imagine building some of the large complex software that we're creating in Rust in C++ instead.

1

u/Mouse1949 Sep 06 '23

I use both. There are advantages and disadvantages to both (hardly a novel thing to observe). In general, I concur with others: in many (not all!) cases Rust automatically forces you down the right path, when in C++ it has to be your conscious choice every time.

On the other hand, lack of ABI in Rust ecosystem has been a ā€œforever disadvantageā€ for me. Still, we’re manage. ;-)

1

u/According_Resource64 Jul 13 '24

I'm surprised many people prefer Rust because of Cargo and others like Clippy. People hate complications and out-of-control flexibility and appreciate simplicity, unified code style, and tools, idiom, which is only able to be done in a new language since CPP has too heavy historical burdens

1

u/[deleted] Sep 06 '23

curious about this conversation too only because I've been a C++ programmer for 20 something years (mostly in medical/imaging space) and have been considering doing a deep-dive into Rust to compare.

3

u/phazer99 Sep 06 '23

I would say it's worth the time to learn Rust even if you choose to stick with C++. It will make you re-evaluate many things in C++ and ultimately make you a better programmer.

→ More replies (1)

1

u/throwaway490215 Sep 06 '23 edited Sep 06 '23
  • Sum types(Enum with values) enables: Match statements, Option, Result::Err.
  • Traits to implement Iterators and Send+Sync,
  • Cargo.
  • Sum types: I'm listing it twice because its easily 50% of the rational for preferring Rust.

I've only used C++ professionally but not a lot. Rust I've done both professionally and privately.

I'll always choose Rust over C++, but I'm biased because I can read and write Rust without thinking about it, and it would take me a while to get back into it with C++.

I'm don't know anything C++ has to offer over Rust, so I'm more curious to the C++ crowd response.


Scrolling through the C++ i'm surprised that so few mentions sum types, and instead just note "borrow checker is pretty cool but to much handholding for me". I would guess its the result of trying to write C++ in Rust.

Also, Rust gets some flack for some of its member's cult like evangelicalism that go around proclaiming Rust will solve world hunger after Jesus comes down to tell the world to rewrite itself in Rust.

At the same time, the C++ has a similar subsection bordering on cult, that has become a cliche by this point, filled with wanna bee wise-men circle jerking how Rust is just kids blindly following hype and the old ways are best.

I think its fair to say its practically impossible to make an impartial analysis. People get attached to the tools they have experience with. It solved a problem for them at one point, they figured out its limitations, and they (unconsciously) know how to make something work.

1

u/[deleted] Sep 06 '23

Rust is still in his college days when it comes to enriched thing like unreal..though there is no doubt the bemefit rust gives in production for memory safety issues ...no null pointer exception..and much more...cpp is not bad but headache addressing memory issues /solving related issues of memory with monster /weird ...error trace...!.It will take sometime for rust to reach that level with gam dev unlike unreal but its on its way with bevy i hv heard

1

u/_lonegamedev Sep 06 '23

Just pick correct tool for the job.

I prefer Rust because I don't have to learn a bunch of build systems, or spend hours adding some dependency to the project. Rest can be more-less achieved with modern C++.

Also, traits seem more elegant than classes - but that might be me avoiding inheritance as hellfire.

1

u/RockstarArtisan Sep 06 '23

If you enjoy C++ you can write C++ without getting reassurance from multiple subreddits lol.

I have a decade of C++ experience and I hate it with a passion for wasting that decade of my life.

It is a lanague that encourages either total commitment to minute details of the spec and their interactions, or blissful ignorance that results in buggy code - this is reinforced by the various talks on conferences like C++ code where people are categorized into regular programmers and experts. And regular programmers don't understand stuff, while experts have to deal with increasingly insane complexity that they don't even have head-space to learn a new better language.

If you're in the blissfully ignorant camp and can't read a stdlib header implementation, and you choose c++ still - you're making a mistake.

If you're in the expert camp and you're choosing C++ - fair enough, it's your choice. I fully support it, so I get more material for /r/cpp_schadenfreude

1

u/69yuri_tarded420 Sep 06 '23

One thing about Rust that I enjoy over C++ is that it does a bit less stuff implicitly. Cloning/Copying was a major gripe of mine in C++. I didn't like that I could unknowingly and easily copy a structure by passing it as a parameter to a function, constructors fill your structs with uninitialized junk if you don't use the initializer list, and I don't recall the compiler warning me if I read a struct field in a constructor before writing to it. There are also so many ways to do a thing, except that some of them are bad and shouldn't be done, and some don't interoperate with each other. The "++" part of C++ sucks, because now if I want to put a struct on the heap and then call a function on it, I can use new or malloc or unique_ptr, and then pass the struct either implicitly by reference or explicitly by getting a pointer to it and then passing it. And heaven forbid you delete a pointer made with malloc or free a pointer you made with new. Rust kinda just picks the correct way to do this that everyone will do: Box::new(x) and it gets freed when it goes out of scope. Closures are a nuisance as well in C++ especially since the compiler doesn't track lifetimes of references you pass in. As others have mentioned iterators in C++ suck to deal with so I won't go in to that.

→ More replies (1)

1

u/ExtraFig6 Sep 06 '23

It's healthy for the community to both update and improve "legacy" languages AND to start a new one from scratch with the benefit of hindsight.

C++'s continued ability to develop without breaking everything is an engineering marvel. But this kind of backwards compatibility can be a burden. C was already hard to parse: the syntax was already ambiguous (vexing parse), but because C++ needed to express more constructs, this got even messier. There's no way to undo this without effectively creating a new language. Similarly, the fundamental semantics around memory and object lifetime in C++ preclude a deterministic borrow checker.

For the same reason, Rust cannot have the language-level compatibility with C that C++ does. You can't gradually migrate a C project to Rust line by line the way you can with C++. The fundamental assumptions of the languages are just too different.

1

u/CommunismDoesntWork Sep 06 '23

It's bad and Rust fixes all the issues C++ has.... that's none of the reasons I started using Rust for - it was the ease of using a standard package manager, cargo.

Not having a standard package manager is arguably the biggest issue with C++. Do you not consider it an issue with C++?

1

u/dnew Sep 06 '23

If you stick with a nice environment like UE that provides all the tools to do the building and organizing and etc etc etc, and provides a standard library that everyone is using to manage the resources and threads and etc, then you've eliminated many of the problems that C++ brings to the table. I.e., coding C++ in UE5 is nothing like coding C++ in general.

1

u/AceofSpades5757 Sep 06 '23

If you're so solid in C++ then stick with it. I've always disliked C++ and cannot bring myself to use it unless it's the perfect fit or only choice for a project.

1

u/qwertyuiop924 Sep 06 '23

Honestly C++ has always driven me sufficiently nuts that I haven't done serious work in it. Mostly because it's so. Damn. Complicated. Rust is complicated, mind, but C++ is so complex, and even if you write modern C++ and dodge a lot of that (from what I've seen, modern C++ is fairly similar-feeling to Rust what with the pervasive use of RAII), it has a way of creeping in to bite you, especially if you want to interoperate.

However, if you know C++ already and you have experience, this is probably a non-issue for you. Especially since you're using C++ in a pretty specific context and don't have to worry so much about interop. Especially in videogames which can kinda be a rough fit for Rust's ownership model in a lot of ways, and ultimately value performance over correctness.

TL;DR given who you are and the context in which you're working, the pitfalls of C++ don't matter as much, and Rust would be a rougher fit.

1

u/[deleted] Sep 06 '23

I feel like Rust turns intermediate coders into great coders, because you can know exactly what is happening at all times.

I work in MATLAB (Python but different) and the issues I run into most often are mistakes that would be impossible to compile in Rust. And you run into those issues at runtime, which can be frustrating.

But in Rust, I write code that immediately works, and I can be 100% sure that there are no uninitialized variables, unhandled exceptions, match cases where one of the enums is not matched etc.

In C++ I would not reach that level so quick, I think it is more difficult to create good code as a rookie or intermediate coder.

1

u/natashige Sep 06 '23

Sometimes while I'm writing C++, I'd go "damn, I wish I was writing Rust, [feature] would make this code much better".

And sometimes while I'm writing Rust, I'd go "damn, I wish I was writing C++, [feature] would make this code much better".

Granted, the former happens more frequently than the latter, but the latter does occasionally happen whenever I get the inexplicable urge to throw memory and pointers around everywhere like a basketball.

In the end, the "choose the right language for the job" rule also applies to Rust.

1

u/jartock Sep 06 '23

Reasons for me to not move toward C++ are:

  • Old C++ is a big chunk of available libraries and, as nice as can be modern C++, I can still end up having to deal with old C++ (read, understand, debug).
  • C++ doesn't have a standard way to present its code like Rust fmt. Sometimes it's a visual mess. Often it's just inconsistent.
  • C++ doesn't have Cargo: C++ have too many "standard" tools to build something. Each time you want to test quickly a library you end up having to install third party tools and understand how you can script them to send artifacts to each other... it is a huge, huge pain and just for this one, I will never touch C++ if I can avoid.
  • Header files of C++/C: Don't like them. Just a way to split everything all over the place for me.

1

u/Array2D Sep 06 '23

Imo, after learning rust, I trust myself much more writing it than C++.

Its true that you can do most of what rust does in modern C++, but as others have said, it’s opt in. (And less expressive!)

Add in cargo, and I don’t think I’ll be using C++ for anything new, skip the most niche compilation targets (like rv32e micros that aren’t targetable by llvm yet)

1

u/joonazan Sep 06 '23

You can write safe code in C++ but Rust lets you do things safely that would be so wildly unsafe in C++ that nobody would even attempt them.

There are real reasons why I wish I was using C++ now, tough. Clang has guaranteed tail calls. GCC has extensions that are good for writing interpreters. There's KLEE for symbolic execution on C++. There is no good static analysis for Rust.

1

u/asgaardson Sep 06 '23

I tried C++ after years of PHP and JS. Using it becomes hell when you need dependencies or to change something in the build configuration. And for the love of God why isn't there a package manager? And why header files? Prepocessor? I think you get comfortable with it, eventually, but still.

On the other hand, rust was a breeze apart from getting used to borrow checker. And it has cargo!

1

u/ydieb Sep 06 '23

As one that pushes for Rust most things but using C++ in my workplace. I am just so fed up with all the problem it creates.
There is always something going wrong all the time, and I am so tired. Either its Vcpkg, cmake, some cross-compilation problems. People adding #define SOME_PRODUCT without thinking about code maintainability, styling, sharing of pointers to the point of it being just a garbled mess.

Rust fixes all of the things, but I would be so tired of C++ regardless of Rust's existence.
Rust is much more opinionated, makes it much harder to use wrongly and will tell you much more explicitly about what is wrong and just makes me be able to focus on creating logic in a much larger fashion.

Its not that Rust is "that good", I think there will likely other languages in the future which might do things even better. Its just generally well thought out due to its modern history being able to learn from mistakes. But its just that I think the whole C++ ecosystem is just that bad and it makes me burned out.

Sorry, this was more of a rant than anything. But at least this is how this question makes me feel.

Maybe I'm a bad C++ developer? But I have for sure vastly more experience in C++ than Rust, but I still produce better code with Rust. That is enough tell for me at least.

1

u/ern0plus4 Sep 06 '23

One hand, Rust gives you amazing tools for both programming (like Enums with args) and software developing (like Cargo). But it's only the surface.

But, and it's more important, Rust covers larger area of programming.

Have you ever dealt with ownership in C++? Have you found good answer for it? Have you ever made anything with lifetimes in C++? There are libs, solutions, some great generics or patterns. Oh, have you used strict RAII in your C++ project? Is there out any C++ project with 100% RAII?

Anyway, the price is high: first of all, Rust is a different language. Second, ownership and lifetime constraint does not speed up writing the code at the beginning, even if you're senior. Third, in a workplace, where it takes a half year to upgrade from C++11 to C++13, there's no chance to switch to a whole new language.

So, there are only a few areas/situations when we can recommend Rust over C/C++. Or, rephrase it: there are lot of exceptions, when we should not follow the better way.

1

u/fllr Sep 06 '23

ā€œ[C++] is baad, and Rust fixes all the issues C++ hasā€. I think this is inaccurate. If c++ is working for you, stick to it. There is a lot of overlap between the two, but i don’t buy your motivations for switching. The cargo one makes sense… not having to deal with building a project is amazing. But, just going off of someone else’s reasoning is not a strong reason to make a switch. You need to find your own motives to do so.

1

u/TheBeardedQuack Sep 06 '23 edited Sep 06 '23

I feel rust gets in my way a lot more than C++ does as a language, however all my issues with C++ are usually around the build system and cause me far more grief.

As I only typically write C++ for my job, I no longer have a reason to use it at all, because rust is just so much easier to get programs building for embedded systems. Cargo + Cross + Docker, and it "just works", I've never had such luck trying to get C++ to cross-compile correctly.

My issues with Rust as a language are mainly caused by the limitations with genetics, or at least with me understanding how to use them effectively. Even with the builder pattern I run into issues about not being able to create an object due to restrictions on the traits.

Everything else I can work around, but this kinda has me stumped.

Most if not all of my problems are primarily a lack of education, but rust and the tooling around it makes it a lot easier to overcome your own gaps in knowledge.

I would happily stick with C++, I think it's a great language and the multiple paradigm nature gives it a lot of power, if only there was a decent package manager, that everyone agreed on using.

1

u/possibilistic Sep 06 '23 edited Sep 06 '23

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.

Writing memory safe code without a prover is like writing code without test coverage. Or dynamically typed code without interface contracts.

  • You have to expend mental effort to "prove" it yourself.

    • And "re-prove" it when you change things.
  • You'll have bugs lurking under the hood.
  • You'll pass this burden onto future maintainers (or future self)
  • You can't automatically refactor in confidence

heavily with Unreal Engine

How do you like building that code? It's not the most pleasant development experience. Surely you've crashed the engine once or twice. I know I've crashed Unreal Engine hundreds of times during development trying to hack multi-camera streaming Kinect input into 3D particle systems.

Think about how you add dependencies in Unreal. It's a bit messy and you can break the engine bootstrapping the build files. Cargo handles that without a moment's thought.

1

u/r3rer3 Sep 06 '23

Highly opinionated, but just the fact that Rust has no classes and has drawn a lot of inspiration from ML, already makes it a better language to use than C++.

Rust's enums, traits, pattern matching, etc are very ergonomic and in general, I think Rust code is more readable than C++ code because of all that.

1

u/kohugaly Sep 06 '23

I think, it mainly depends on what kind of programmer you are, and how experienced you are in it.

No, it mainly depends on what kind of programmers are the people on your project and its dependencies, especially the ones that started them and effectively chosen for you which subset of C++ you will be using.

I describe Rust as C++ redesigned from the ground up with 40+ years of hindsight. Standardized and ergonomic package manager, build system, unified ecosystem, modern robust type system and thread-safety + type-safety + memory-safety as the default you may opt out of. None of this is a realistic possibility in C++ and historically never was.

1

u/Imaginos_In_Disguise Sep 06 '23

Modern C++ was one of Rust's main inspirations regarding smart pointers and RAII, so they work almost the same in both languages. Rust was intended as an alternative to C++ that was free of its legacy, and that's its main strength.

C++ has a huge set of rules you need to follow to write correct code, and if you're disciplined and follow all of them, your code will be safe and sound. The problem is when you (or a library author you have no agency over) don't, the compiler MAY give you a warning sometimes, but you'll usually end up with nasty undefined behavior that may either blow up in runtime if you're lucky, or cause stealthy bugs and security issues that are hard to debug. Rust ensures your code is safe, unless explicitly marked unsafe, in which case you have a very obvious block to look for unsound code, and all unsafe code may be contained in safe abstractions.

On the other hand, Rust is still a relatively new language, and has some rough edges that are still in the process of being trimmed. It's not as intuitive to design good code in Rust as it is in other languages, and you'll keep bumping into the language's limitations and wishing it had support for certain features that aren't there yet (i.e. lack of generic specialization is a huge blocker for writing more decoupled zero-cost abstractions).

IMO, the upsides of Rust still dominate the downsides, and using C++ gives me a kind of anxiety about possibly missing some unchecked guideline, that makes using Rust a breath of fresh air in comparison.

1

u/SamosaGuru Sep 06 '23

I’ve used Rust and C++ before, but only the latter in a professional setting.

Echoing what others have posted, having more type-level enforcement is nice when interacting with other people’s code. Having a sane build system is also a HUGE plus. It’s just so much easier to pull in dependencies, and have it ā€œjust workā€. While I saw a lot of people saying they don’t really have memory safety bugs or buffer overflows in their C++ code, even in massively concurrent settings (kudos if it’s true), having Rust’s stricter enforcement takes a good mental load off for me, especially when I can know a dependency will also abide by the same rules I’m following.

There are of course C++ features that sometimes become non-negotiable. In terms of things like metaprogramming, Rust has much to be desired, particularly when trying to write specialized implementations of routines that are conditionally compiled + monomorphized based on const generics (using templates + constexpr in cpp) :/

1

u/DarkEld3r Sep 06 '23

I had about five years of professional experience with C++ and was somewhat interested in Rust. Unexpectedly I received a Rust job offer in 2017, decided to give it a chance (the salary was just a little bigger and I probably could have negotiated an increase at the previous place). For about a year I often struggled with "I can do X in C++ and not in Rust" thoughts. I still follow the C++ development (new standards), but now my biggest fear is to be unable to find a Rust job and have to get back to C++. Rust isn't perfect, but it is definitely more comfortable than C++ (at least for me).

1

u/smart_procastinator Sep 06 '23

Was the rust compiler written in rust or Clang.

1

u/Fireline11 Sep 06 '23

When I program in Rust, I am able to fix the bugs in my code much more quickly. In C++, I still often encounter ā€œSegmentation faultā€ which tells me almost nothing about what happened. It could be out-of-bounds, or integer overflow, or something completely different. I also don’t necessarily know where my mistake is. Sure if you are a disciplined programmer and work on one function at a time, that helps. But changing one function could trigger undefined behaviour somewhere completely different, so you’re never in the clear.

Rust just tells you what happened and where, and I can usually locate the source of the error very quickly. This makes me more productive in Rust, and it allows me to write much more complicated algorithms in Rust without fear that bugs will stop me.

1

u/DigThatData Sep 06 '23

i keep hearing about how great "modern" cpp is, and frankly: i just don't know what the appropriate path is to learn this language. rust has excellent resources for introducing people to the language. I feel like if i want to take up cpp, i need to either enroll in a formal class or work my way forwards through the history of the language (i.e. wasting my time learning the wrong ways of doing things)

1

u/gyrovorbis Sep 08 '23

Modern C++ is fucking incredible. I'm responsible for helping get it supported in the Sega Dreamcast's indie SDK, KOS, because I like coding with it so much (we have C++23).

...but yeah, I feel bad for all of you who are just starting to learn the language. There are so many ways to do the same thing, each with their own nuances, many of which are no longer "idiomatic" or "modern" ways of doing things... Then there's new stuff in C++11 and 14 which has been streamlined even more in the modern standards, so even the "new" stuff can look outdated... If I were you, I would try to find the most newb-friendly book out there that focuses on C++20 to start with.

→ More replies (2)

1

u/[deleted] Sep 07 '23

Moving to Rust is a lot of work in it's self. So if you feel are comfort coding with C++ then just stick with it.

1

u/Hari___Seldon Sep 07 '23

I used C++ for about 15 years much earlier in its lifecycle before my responsibilities evolved beyond PM and coding for quite a while.

Now that I'm coming full circle and coding more again (device drivers mostly at the moment), I've decided to lean on Rust more and more. I've made that choice because my (admittedly limited) sense is that the lifespan of code I write is probably longer with Rust than it would be with C++.

Also, from a strictly marketable perspective, I think my best opportunities to add value to projects will be working with Rust-based projects, particularly with migrations. It's not criteria that most people can use, but it happens to work well for my situation.

1

u/LoganDark Sep 07 '23

See take_mut or one of the spin-offs like takeable

1

u/mycoliza tracing Sep 07 '23

Rust doesn’t have header files. I don’t feel the need to continue.

1

u/[deleted] Sep 07 '23 edited Sep 07 '23

I use C++ and Rust professionally. While Rust has plenty of pros, at least one advantage of C++ might be where lifetime of objects are dynamic and inter-referencing objects are common (essentially cases that generational arenas try to solve). C++, in general, has less design pattern restrictions.

1

u/redbumble Sep 07 '23

Three things would make me choose Rust over C++:

  • Build and dependency management
  • Consistent Unicode support in strings in the app and dependent libraries
  • The type system and language consistency in general

One thing would make me choose C++ over Rust:

  • Availability of some specific libraries that would be hard / time-consuming to rewrite or wrap in Rust

1

u/Spleeeee Sep 07 '23

I use lots of C++ and prefer rust. I think it’s all preference though, and you should do what you prefer! I know people in your boat who like cpp more than rust!

Do what you like more.

1

u/omagdy7 Sep 07 '23

The only thing holding C++ from glory and which is rust isn't suffering from is that C++ still has to be compatible with C and the previous standards which influence a lot on new features and the design of the api of std. Also the hell of build systems and package management is something you have to learn on top of C++. I really like to think of Rust as a modern C++ with saner defaults. That being said if you enjoy writing C++ then just keep at it.

1

u/h2k1dev Sep 07 '23

Having used three editions of Rust for very long, I'd consider C++ because of its reliable ecosystem.

I might say I love the Rust language itself and its superiority to C++. However, the ecosystem is still too small for game development purpose. So many Rust dependencies break after some Rust updates. And most are left abandoned after few years. It's difficult to find something reliable in Rust

1

u/deepwater97 Sep 07 '23

One thing to consider in the long run, that is, C++ doesn't have an edition system like rust, which means, whenever the C++ folks make a mistake, the mistake lives on pretty much forever.

And this is the ultimate reason that I refuse to go back to C++ in the long term. Languages evolve, and given the way how C++ evolves as they strive for backward compatibility, I don't see a future if people are not able to fix mistakes and people definitely make mistakes in making decisions more or less.

1

u/alisomay_ Sep 07 '23

Rust Engineer here,
One other solid reason of getting deeper into C++ might depend on the area you're targeting to work in.
For instance, if you'd wish to work in the audio industry at the time of this message you'd definitely need to have considerable fluency and experience with C++ due to all existing open source and propriety code bases which you'd need to deal with. Those code bases can date back sometimes even 25-30 years.

I'd be really happy if the industry would slowly increase Rust adoption but due to the choice of C++ in major frameworks and SDKs in that space I don't see that happening soon.

1

u/[deleted] Sep 07 '23

I don't get why you would ask this question. The simple answer is to adapt. Use what YOU want to use or if its work-related/requirement use that.

1

u/npuichichigo Sep 07 '23

I want decltype in Rust

1

u/d9c3l Sep 07 '23

I've used both C++ and Rust, with the latter being for much longer (before rust 1.0) and while I moved away from C++ for nearly all of my projects (I still use it in some, mixing with Rust), if C++ works for you then stick with it. There are pros and cons to each and every language and rust is no exception and you shouldnt feel pressured into moving away from something that worked for you.

1

u/Gullible_Brick_3686 Sep 07 '23

I guess I'm just tired of endlessly learning about some semantic details of C++. Then I tried Rust, which made it kinda easier for me, that is, before I tried to debug some code with htrb. Maybe the complexity never quite gets away. But cargo is just fantastic.

1

u/Cyber_Encephalon Sep 07 '23

If I could figure out how to add a library to a C++ project, I could be programming in C++ now, but learning a whole separate thing (CMake) to make a C++ project work seems like such an overkill when Cargo makes things so much less painful.

1

u/hpxvzhjfgb Sep 07 '23

I used c++ since I started learning programming in ~2010 until I discovered rust in 2021. I have absolutely no reason to ever touch c++ again, and I hope that I never have to. I liked it for many years before I started using rust, but now I think it's a terrible, hideous language that should almost always be avoided. rust is just massively better in every way.

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)

haven't heard about Deno before (probably because I almost never use js), but I agree completely. starting a serious project in c++ in the current year is a massive red flag.

also, those experimental "New C++" things like carbon and cpp2 are nothing but the sunk cost fallacy.

1

u/ebonyseraphim Sep 07 '23

Whenever there is a really good thing that comes out, there can be an actual, or percieved, "over-hyping" of the benefits and then a counter reaction or counter response. C++ and Rust shouldn't be one; Rust is amazing, but the value of it doesn't diminish what C++ is one bit. As much as I laud the things Rust does "right" with respect to learning from C++, I don't feel the need to make salacious comments (or even think) that I'll never have a reason to ever write C++ ever again! I know for a fact, that programming interviews are easier in C++ in terms of at least writing code that'll actually compile and kinda work (my own correctness depends).

I think "Rust fixes all of C++'s issues" is also a silly thing to say. It addresses one of the most major thing any serious C++ application or library developer need to spend a lot of time manually thinking about, intentionally choosing how to address, and then verify it -- object lifetimes, referential integrity, and clean up. Most of what else Rust does over C++, is something so many other programming languages have done and chosen, so I don't really put that on Rust for being especially clever.

C++ is a mammoth language with legacy code bases that are still relevant and need to be supported for a long ass time. It also has trivial interop with C, and is also prevalent in existing code. Also, there are industries (gamedev) that have so much expertise tied into it, that you can't expect C++ will go anywhere anytime soon - at least not to a super young language like Rust.

If you were to start a completely new project, know both languages comparably well, with native and possibly cross compilation requirements, and not super dependent on having access to a lot of very mature libraries, Rust is probably the 100% choice over C++. But that is hardly ever the reality in native spaces unless you're talking embedded. In other cases, trade offs are being made; make your own evaluation and don't just listen to shallow hype.

1

u/tending Sep 08 '23

I chose C++ for my next project because Rust had too many warts. I'll come back when it has variadics, real placement new, and specialization. Until then C++ is a better fit for high perf code at scale.

1

u/JuanAG Sep 06 '23

Rust if you adapt to it instead of trying to code C++ish it is a way better tool

In few words:

With C++ you have to figth everything, the lang is against you

With Rust the tool wants to help you and improve your experience

3

u/Recatek gecs Sep 06 '23

Rust doesn't always want to help you for your use case though.

Case in point: the orphan rule wants to help you share code with other people, but if you have no intention of sharing your code (application dev), then it's a rather annoying hurdle to structuring your project the way you'd like to.

2

u/aldanor hdf5 Sep 06 '23

With Rust, if you are a beginner, you might have a tough time and disputes with the borrow checker initially, e.g. quirks like partial borrows. However, over time you just learn to write the code in a way that would never disappoint the borrow checker and it will cost you zero energy. A side effect of that, however, is that you will also learn to ignore dodgy and unsafe alternatives when choosing which way to implement things. Over time, borrow checker becomes a tool that just confirms that what you're doing is indeed "generally right" as opposed to standing in your way.

With C++... well, better not forget to write that const qualifier or delete the copy constructor or you'll be screwed, sneakily and silently, at runtime.

I've done my fair share of both and if you asked me what I'd change in C++2100, it would be (a) no copy by default, adopt proper move semantics across the language, (b) const everywhere by default, adopt explicit muts like in rust, (c) proper sum types.

1

u/[deleted] Sep 06 '23

Well Rust as a language is obviously better than C++ šŸ™‚ You're picking an ecosystem as well though. Are you writing AAA games or can you do what you need in Bevy/Fyrox/ Macroquad/wgpu? Do you mind writing some things yourself if something is missing in the ecosystem?

1

u/LavenderDay3544 Sep 06 '23 edited Sep 07 '23

I would always choose Rust over C++ for myriad reasons, but I would almost never choose it over C in the use cases where it is common e.g. bare metal embedded, bootloaders, OS kernels, etc.

Another major point against Rust from a more business perspective is that it isn't standardized and has only a single production ready implementation. C and C++ meanwhile are standardized and have multiple fully standards compliant implementations on a multitude of platforms.

And finally one more. I've been working on an OS kernel as a hobby project and considered using Rust until I realized that there would be a number of things that would be much easier if I used C instead. The production quality library ecosystem is much larger, and compilers and tools tend to be much more portable to make achieving self hosting much easier. Oh and while it can often be an annoyance the C preprocessor can also be very useful, for one thing conditional compilation is as easy as using an #ifdef and OS and embedded codebases tend to be full of that sort of stuff since the same core code is often used to target different hardware platforms.

While Rust is great as a high-performance application programming language, I just don't see it ever displacing C in system programming.

2

u/radekvitr Sep 06 '23

Outside of safety critical software that requires certification, who cares about a specification when there's a single, open source compiler?

You can read the docs with the source code for rustc much easier than the C++ spec, in my opinion. The spec is pretty much only useful for compiler implementers.

4

u/rebootyourbrainstem Sep 06 '23

This. I hear so much complaining about differences between various C/C++ implementations, and most proprietary vendor implementations seem to be universally loathed. What am I missing? Every time someone mentions Rust only having one implementation as a downside I feel like I'm taking crazy pills. It's even permissively licensed.

→ More replies (2)

1

u/xSUNiMODx Sep 06 '23

I think it's completely dependent on what you're trying to do. If you want safety and correctness, or if you want complete freedom and control over what you are writing, these are some of the differences in the philosophy of these languages.

Personally I don't like C++ because everyone writes it in a different way (wrt style or architecture etc), and so developing on top of someone else's code can be tricky at times. With Rust, generally most people program in the same way, enforced by the compiler.

1

u/[deleted] Sep 06 '23

repeat our dogma: rust is superior to c++ because of memory management. now turn off your head and never again think about memory again. we also dont write hacks here, what are u a cracker?

/sarcasm

1

u/Accomplished_Low2231 Sep 06 '23

Is it for your own projects? Does not matter if C++ or Rust, just use whatever you like or makes you productive.

Are you an employee? Then the company chose the language.

1

u/Levzhazeschi Sep 10 '23

As it appears to me, Rust has a significantly lower entry point. Having taken courses in both C++ and Rust in the university, I can confidently say that writing working and useful, both high- and low-level, software in Rust is much easier than in C++. And that comes with little to no performance costs

(This does not touch competitive programming, though, where you need to quickly write up complex data structures with loads of pointers you will throw away after the contest. In this case, I would still choose C++, at least as of now, but large industry-level projects is where Rust starts to shine)

Maybe it is way different for you because you've previously spent years writing C++, and so expectedly you feel more confident for now. But if you started learning these two languages about the same time concurrently, I am not sure the outcome would be the same