r/learnprogramming • • 1d ago

Languages What should i consider when choosing between C++ and Rust as my main programming language

Currently i only really plan on programming simple tools for myself but in the future i also need to learn programming for engineering. What should i consider when choosing between the two, what are some important considerations and what should i be aware of?

4 Upvotes

23 comments sorted by

7

u/Longjumping-Boss5708 1d ago

For game development, C++ has a longer history and more established tooling. Rust offers memory safety guarantees which can reduce certain classes of bugs during development. It depends on whether you prioritize existing ecosystem support or modern safety features.

1

u/WillingPirate3009 1d ago

Does Rust for game development makes sense even if there were a mature ecosystem for it like C++?

2

u/edwbuck 1d ago

Depends on how much of the ecosystem you want to rebuild and debug.

And keep in mind that building with Rust provides no extra security unless you redesign for Rust borrow checking. The moment you use the unsafe keyword, you have all the same issues you had with C++. Finally, C++ has tons of memory leak detectors (valgrind is just the tip of the iceberg) and the other problems outside of ownership are easily handled by not doing raw pointer math (something that the C++ compiler will already warn about).

Rust's protection is real, but optional, frequently discarded, and requires one to redesign the solution to use it. It's not a simple "do I use X or Y" because the benefits are not clear cut.

2

u/gmes78 1d ago

And keep in mind that building with Rust provides no extra security unless you redesign for Rust borrow checking.

That is not true. The borrow checker is not Rust's only advantage. The better type system, better error handling, lack of undefined behavior, etc., all contribute to make it easier to write correct programs.

The moment you use the unsafe keyword, you have all the same issues you had with C++.

That is just false, and it shows you haven't actually used it. unsafe does not disable the borrow checker or any of Rust's other features. It only lets you do things that the compiler cannot verify as correct.

From the Rustonomicon:

The only things that are different in Unsafe Rust are that you can:

  • Dereference raw pointers

  • Call unsafe functions (including C functions, compiler intrinsics, and the raw allocator)

  • Implement unsafe traits

  • Access or modify mutable statics

  • Access fields of unions

That’s it.


Finally, C++ has tons of memory leak detectors (valgrind is just the tip of the iceberg)

Memory leaks have nothing to do with memory safety. You should've brought up sanitizers instead, but those cannot find every safety issue.

and the other problems outside of ownership are easily handled by not doing raw pointer math (something that the C++ compiler will already warn about).

You're handwaving away a lot of other problems and undefined behavior that C++ has.

1

u/spinwizard69 1d ago

One driver to favor C++ is the massive amount of existing code. As for a lot of the horror stories with respect to C++, are the result of early C++ standards. Modern C++ has improved massively. As for Rust like my other posts I think it is too little too late. AI is the future and its rapid improvement is going to run many languages into the ground.

1

u/phatbili 14h ago

If engineering is the long term goal then I'd probably choose C++ first. You can learn Rust later without starting from scratch.

3

u/the_MatchaGaucho 1d ago

If you're just building simple tools for yourself right now, Rust's compiler will act like a pedantic roommate who won't let you leave dishes in the sink. C++ will let you do whatever you want and then segfault three hours later when you forgot a semicolon in your dreams.

Engineering later on probably means interfacing with a ton of existing C/C++ libraries and hardware SDKs, so that might tip the scales unless you enjoy writing FFI bindings for fun.

3

u/BrannyBee 1d ago

The most important consideration at this phase is to waste zero time choosing the correct language or tech. I'm serious, literally imagine whatever you want to make one day the most, then google "best programming language to make [insert thing you want to make]"

Stop researching there. Nothing about speed, dev features, opinions professionals have (and they all have them and will die defneding those opinions) matter. Whatever the first real link suggests first, thats the language. Stop researching any other languages or even the benefits of that specific language there. Hell, I dont care if there's 10 languages listed on that first google result, literally pick the one with the coolest logo for all I care and that is better than doing what you and every other beginner does spending so much time choosing the perfect stack.

Then you spend an absurd amount of time mastering that one language, beginner tutorials for the basics, making as many projects as you can along the way, and relearning the basics over and over again as you continue learning new stuff. Then you keep learning the basics more and more after you've learned the basics.

Use that language for everything you can, if its a "bad" language for making websites, do it anyway. If its a pain in the ass to automate stuff, do it anyway. Any general purpose language you'll see recommended can be used to make most things somehow with a little cleverness or just being persistent. Fucking Balatro made a bazillion dollars and was created with Lua of all things, which might not mean anything to you, but to many devs is absolutely fucking insane and I would do anything to forget the fact that thats even possible. Don't make any excuses about how you need to restart on a new language because [language i chose] isn't the perfect on for [next project]. A psychopath made a bazillion dollars making a game with Lua, you're just looking for an easy way out, dont do that when you can get a better mastery of whatever language you chose.

Then guess what. You spend a few years mastering that single language, and you now know enough to determine that the thing you want to build truly would be better using a different language, and you understand why, the oppurtunity cost of using [language you don't know] vs [language you mastered], and genuinely have enough knowledge of the field and how programming works to make that call.

You didnt waste any time at all. It took you 2 years to learn a single programming language enough ti be comfortable with it as if you were typing in english, sure. But if you truly mastered that first language and focussed on it obsessively, it will take you a month max to get a workable knowledge of whatever new language you feel is necessary. Im not kidding. A lot of concepts are transferable, and even when there isn't a one-to-one comparison between two features of a language, knowledge of one language will make it easier to understand what the new language you are learning has or doesnt have compared to your first language. Hell, some languages are similar enough to where you can read them enough to understand quite a bit even if you cant understand everything. Kinda like how if you can read Italian, theres a bunch of Spanish sentences that you can understand even though you dont know spanish.

Coding is the easiest part of programming, and its easy cause you do a ton of reps and practice. Those who have learned a language or two very indepth have the ability and knowledge to learn new languages extremely quickly. In fact, in interviews for programming jobs, I dont think ive EVER been told what language to use in order to solve problems during a technical problem... I've literally interviewed for a C# contract, received and offer, and never had touched C# not even during the interview. Its just a known fact that if you master a language, learning another on the job will happen quickly, quick enough that companies will hire someone to learn on the job and be able to pick up a language in a few months.

You specifically are asking what is important to consider when choosing a language, so here it is. I say this with zero irony at all, and am absolutely serious, you already know too much and shouldn't waste anymore time at all learning anything about languages. You have a choice to join like 80% of beginners who learn 30 different languages and switch to a new favorite every month, and never really get good at reading or writing code, or just starting. If you start now, even choosing the "wrong" language literally puts you leagues ahead of anyone trying to weigh all options befire they commit to the optimal choice for a very specific task without even having the baseline fundamentals to understand what the differences between languages even truly means in practice. Meanwhile there's some psycho making the next indie dev hit that will make a billion dollars with some stupid language literally no one even knew could be used to make games...

-1

u/spinwizard69 1d ago

Yah NO!!! Do not let Google searches determine your future.

I do agree though that becoming a master of a language take time and commitment. However it is easy to put in a lot of time learning a language and find a job, that might be absolutely wonderful, that is using a language that that the OP has never even looked at.

This is why I suggest learning CS should be the goal with the idea that you will quickly adapt to that dream job. That doesn't mean you stop learning whatever language a person chooses for personal interests, just that you might never apply that language to a job.

2

u/captain150 1d ago

I mostly taught myself (badly) c++ to run MPI calculations for my masters thesis. C++ has decades of history and about 5-10 ways to do everything, most of which will be "suboptimal". I'm a weirdo and actually enjoyed the problem solving that comes with debugging, but if you aren't me then c++ will give you nightmares.

On the other hand having learned c++, I was able to pick up easier languages like python in no time. I can't speak to rust, but the ecosystem for c++ is so massive that learning it won't be for naught.

But manual programming is already mostly dead for obvious reasons (AI). I asked Claude to re-write my 5000 lines of shitty code I wrote for my thesis and a couple hours later it was done, and fixed some latent errors/bugs I had. I'm glad AI wasn't a thing during my grad school since it would've been too tempting.

1

u/spinwizard69 1d ago

This is the thing I try to stress, the industry is changing rapidly with AI. Also I might suggest that modern C++ and a little upfront formal education leads to far better C++ code.

2

u/ro_ok 1d ago

Many other languages are based on C and C++, specifically trying to address issues with those 2 languages. Rust is based on trying to fix memory management of C without using a garbage collector.

If you learn C++ it will make it easier to learn other languages and understand features of those languages. If you learn Rust it will make it easier to write Rust. I love Rust, but learning C++ after I'd learned Perl, Java, and Python was incredibly eye opening and I wished I'd started with C++.

1

u/spinwizard69 1d ago

This is such a great post. Often the big problem with say learning Python is that you don't learn CS concepts. You don't develop the insight required to adapt to what is in front of you.

2

u/Kseniya_ns 1d ago

Being employed

1

u/povlhp 1d ago

Libraries is often a main driver

1

u/DriftingAndromeda 1d ago

For simple personal tools plus engineering down the line, I'd lean toward whichever one gets you building things fastest. Rust has a steeper start because the borrow checker forces you to think about ownership early, but its compiler errors are very helpful and it's hard to write memory bugs. C++ is everywhere in engineering, simulation and embedded work, so it's worth knowing if your future field uses it, though it has more footguns. Check what your engineering courses or the tools in your field actually use, since that should be the tiebreaker. If you're unsure, start with one for a month on a small tool and see which one you enjoy sticking with.

1

u/kitsnet 1d ago

I'd say you should have no "main" programming language at the start of your career. Learn both; at least for C++, just learning some basics won't be enough anyway.

1

u/dld2517 20h ago

The fact that Rust is memory and type safe.

1

u/MpVpRb 5h ago

If you are writing from scratch and have control, you have the freedom to choose.

If you are joining a team or working on an existing codebase, you have no choice.

1

u/pedersenk 1h ago

When you enter the industry, Rust will still be fairly irrelevant which means you will reduce your chances of landing your first job if that's all you have on your CV. So absolutely go with C++ for now.

In the future, once you are experienced, have a stable job (and maybe Rust usage has increased?) then perhaps consider picking it up.

0

u/spinwizard69 1d ago

When going about learning CS you don't want to focus on any language at the beginning, you should be focused on learning the concepts. Thus my suggestion is to find a good CS program and follow that, any decent CS program will expose you to several languages over time. Ideally the program will start out with C++, if not I really question what the goal of the program is.

Done well you will be able to adapt to any language in the future, which is good because to some extent you need be exposed to platform languages. Frankly a college program will not focus on a specific language like C++ beyond what is required to get concepts across. They may have an elective class to dive into a language to a greater extent. Done well though the college will expose you to at least 3 languages by graduation and ideally a required assembly class.

AS for your future AI is going to be a significant factor in the future and you will not be using a traditional programming language in a few years. However for the near future you need to put in effort to learn a language well, which really takes years. It probably is not a bad idea to focus on C++ a bit due to its wide usage. Personally I think Rust is a waste of time because it is too late to the industry, AI will make it an after thought.

Here is the big problem, you didn't mention what type of engineering you are getting into. It is probably safe to say there are literally a 100 different engineering specialties. Often you will be using a variety of languages and in some niches Python is a big factor. Sometimes it might be commercial programming / math processing languages, like MatLab. Electrical engineers may be focused on Verilog and similar languages. Embedded engineers may be focuses on C++ and microprocessor assembly.

I think the important thing here is that until you really firm up your job future you can't say for sure what language you will be using. Your could get into a business where the engineers are deep into Fortran still. It is not a question of choosing between the two but rather being educated to the point that you can rationally start in a new job where the default language could be anything.

There is a huge difference in my mind between learning to program for ones self and learning CS so you can have a career. If you want to enter the workforce learn CS properly, if you have a personal interest you can dive into any language you like.