I am interested in either updating myself on (C++11>) or going the Rust way. I see a lot of criticism/hate of C++ lately (mostly due to the comittee), but the tooling and libraries are there for multiple areas.
Rust is also interesting to me from a security standpoint.
So I don't really have time to invest in both languages; any directions?
> I see a lot of criticism/hate of C++ lately
Should tell you something. It's not like agenda driven PR campaigns are new to social media. Whenever I see a wave of criticism or praise on social media, I think to myself, what would paul graham do?
If your job is Python/Golang/C why do you want to learn C++ or Rust? Learn neither. Why not learn a functional language or SQL instead? Or delve deeper into python or go or c.
Why?
Many reasons. For starters, C++ is 40 years old at this point and embodies software engineering practices from the late 70's to early 80's (for you young folks, structured programming was the new hotness in the 70's!) C++ was also designed to take advantage of C's tooling, or lack thereof. C had incredibly simplistic dependency management. It worked well for the time since there were very few external dependencies, almost the entirety of the codebase was hand-built by the entity creating the software. There were very few libraries outside of the standard libraries.
C++ has an object model making it difficult to use and extend objects from different code bases/libraries. Never mind the inheritance patterns leading to lots of confusion. Just remember, Java and later C# were created to address these issues with C++. They were both touted to be better versions of C++ (which was true in the application development space where a GC could be tolerated)
Finally, the C++ type system is barely a type system as we understand them today. That's just history - C++ reflects our best thinking from the 70's and the modern type systems started emerging in the 80's. The simplicity makes some things unduly burdensome to implement.
Don't get me wrong - I've programmed in C++ for decades and I like it, warts and all. But I'm also practical and I recognize its days are numbered. Will C++ be around in 20-30 years? Absolutely! But so is Cobol, and would you start a major project today using Cobol? No. You can see we're shifting from C++ to Rust. C++ is the past and the lion's share of the present but Rust is gaining on the present and is the future. If I did't know either Rust or C++ in 2022 then I would focus my efforts on learning Rust.
But—also learn Zig in addition to Rust.
Having seen first-hand how so many threat vectors these days are now supply chain attacks, having reported CVEs and earned P1 bounties in memory safe languages, and having worked on static analysis systems to detect zero day exploits—I'm also impressed by Zig's overall approach towards safety, as being more than only memory safety.
For example, this comes through in Zig's extreme simplicity and explicitness, and also how Zig enables checked arithmetic by default for safe builds. For people who haven't worked in security, integer overflow and wraparound may seem like small things, but they increase the probability of exploits such as buffer bleeds, i.e. attacks like OpenSSL's Heartbleed, which are often remotely accessible and easier to pull off than a UAF.
In fact, no language is 100% memory safe, i.e. able to prevent a memory buffer bleed, because these are logic errors with respect to the protocol. However, explicit control flow and checked arithmetic do help to close semantic gaps and minimize ambiguity, which is what good security comes down to.
Of course, Zig only offers spatial memory safety, and not temporal memory safety like Rust. So Zig is more memory safe than C, and less memory safe than Rust or JavaScript. Nevertheless, Zig is safer than Rust when it comes to being able to handle memory allocation failure.
Again, Rust's borrow checker is also valuable for concurrency safety, i.e. for multithreaded systems, but if you're using io_uring for fast I/O, then multithreading is less of the necessary evil that it used to be a few years ago.
Zig is something you can pick up in a week, with state of the art tooling, and fantastic C-ABI interop for libraries.
So... learn both!
Spoiler: I did this exercise myself. I found Rust to be more convenient in some ways. The Rayon crate made adding parallelism (threads) pretty easy. Some other crates made things pretty terminal output easier.
C++ feels more familiar to me in many ways though - I also write C fairly often. Implementing parallelism was more difficult. (I tried `pragma omp` stuff which should work similarly to Rayon, but couldn’t get it to work.) But just getting something working seems a little more straightforward.
I find myself fighting Rust somewhat often. In C/++, if you know what you’re doing, and you just want something working as a fun side project, it usually takes less dev time in my experience. Maybe that’s because I’m still learning it. Some folks will argue that the Rust compiler forces you to write better code and actually makes dev faster because it catches your mistakes. I haven’t found this to be true.
For example trying to implement simple polymorphism in Rust that was analogous to the implementations in Raytracing Weekend got ugly very fast. I ended in a weird hell of having to add annotations to everything to support generics. Again maybe I don’t understand the language enough, but adding polymorphism was a 5-minute straightforward exercise in C++, even as someone who doesn’t write it often.
I like a lot of stuff about Rust but I’m doing my hobby dev stuff in C instead nowadays, mostly for dev speed like I said, and probably would opt for using a subset of C++ for higher-level projects.
[1] https://raytracing.github.io/books/RayTracingInOneWeekend.ht...
Going to rust directly has been a very frustrating experience and made me give up.
Instead I spent time learning C++, I found out I could more easily transfer my knowledge/skills there. Once you become more proficient with the basics you learn about move, copy, smart punters, ownership, etc.
After some point you start to notice that you are basically doing in your mind exactly what a borrow checker enforce. So that made me curious and I tried rust again, and this time everything made complete sense and the language has been incredibly refreshing to work with.
This intermediate state was very useful for me to build my mental model and develop a better intuition, C++ made it simpler to do it in a more progressive way. And Rust is a fantastic langage if you can wrap your mind around it’s semantic and learn to work with the borrow checker.
With Rust, depending on what you're working on, you may lose almost no time integrating C and C++ (with an extern C interface) libraries, or you may lose 50% of it. Sure, you don't have to know CMake at least (which is a hard requirement for C++ nowadays), but using any extraordinary library dependency in Rust may prove to be a nightmare. Essentially: if someone else didn't already package it, prepare to lose some serious time. The language is nice, but the ecosystem is immature.
Things are probably going to change, and Rust will become more viable over time, but C++ is also improving at a rapid pace so I guess Rust will never completely outcompete it.
Rust is less expressive than C, but likewise it does give less chances to shoot yourself in the foot.
C++ is often derided for its complexity, arising from its long development. While it's true that C++ allows many wrong ways to do something, I don't think that should really be a problem. The language's main problem is societal: C++ (and C) is often used by people who haven't bothered to learn the language. E.g. one can't rely on random unknown C++ code handling integer overflow and type punning correctly, so in such cases it's wise to compile with
-fno-strict-aliasing -fno-strict-overflow
A part of the problem is that C/C++ is so traditionally established, it's taught as as an introductory language at universities, for example. But it's usually taught wrong, because there's no incentive to teaching it right, and trying to teach it right could in fact be counterproductive, if the goal is merely to introduce someone to programming. This is why I think it would be best to teach Python or Julia as the introductory programming language, and (modern) C++ only afterwards.Rust's advantage in this regard is that it's more centralized, it's lead by a foundation instead of being standardized, so everyone who learns Rust, learns the one, correct way to do things.
C++ I find can get a little unwieldy quite fast, but is generally easier to keep simple. It maps to higher-level concepts in easy and intuitive ways. Rust, I hate to say it, but unless you know best practices / idiomatic Rust from the start, you'll have to do a lot of refactoring as you learn the language. Some constructs simply aren't possible using safe Rust. Many embrace the limitations, many else frustrate themselves again and again until 'the Rust way' drills into their heads. But, of course, once you get a feel for it, it's pleasant to write in.
How much difficulty you'll have with Rust or C++ depends on what problems you try to solve, and what your approach to programming is generally. If you're more of a pragmatic "I write simple programs that actually do things" I don't think you'll have any problems with Rust. It's very straightforward. And the tooling is second-to-none. If you like to partake in exploratory programming, trying out different theoretical models and different abstractions, C++ caters to that by allowing you to write more elegant designs than in Rust, with the downside that it also lets you make worse mistakes.
The wider cross-language library ecosystem is mostly C-based I've found, so, in C++ you can just use those libraries directly with no hassle (the only hassle being the build system, or lack thereof). Rust can use those libraries as well (as long as you don't have a gut-reaction "ew" to using `unsafe` everywhere) but unfortunately the safe and idiomatic Rust library ecosystem is kind of on the bleeding edge at the moment. Hopefully things will stable out soon, and all these hip new technologies will come to fruition, but that day is not today.
I know this isn't really nudging in one way or another but really it comes down to what you value in a language and what projects you want to see through using these technologies that will determine which one you like better. Have fun!
Rust is probably a better bet for web-related things. There is not much C++ there, and Rust seems to get some popularity. After all, Rust was originally made for Firefox.
For more industrial applications, or game dev, C++ is likely the way to go. Where I work, we do a lot of industrial applications and we have a lot of C++ projects, including new ones. Rust is not even on the radar. [Edit: C++ here is mostly C++11 now, with some C++98 and not much C++14 and later, I expect that we will get to C++17 soon enough but C++20 probably has a long way to go]
Forget about the criticism/hate of C++ and hype of Rust, it has little to do with practical applications.
Learn Rust because it has things to teach. Learn C++ for what it has, too, and because it is the language work gets done in.
They are much more similar than different. Rust builds in prescribed solutions for what its designers have deemed the most pressing problems. C++ builds in more power for library designers to address what each considers the most pressing problems.
There is broad agreement on what are problems, but with different solutions. Both languages work. I personally find Rust unable to express what I want to write, but it is evident not everybody wants to write those things.
Knowing both will make you a better programmer in either.
Practically all of the highest-paid coding work is conducted in C++. That will be true for many years to come. More people pick up coding C++ professionally, in any given week, than the total paid to code Rust. That has been going on for years, and is accelerating: attendance at C++ conventions, and the number of them, is exploding.
So, keep up with C++, and keep an eye on Rust.
So first learn Rust, later learn C++11+ (by the time there will be a successor of C++11 that may look even less like the C++ you know).
Learning Rust will develop your thinking and taste, and this will then help you judge the flavour of C++ that you will be learning later (and because you'd update your C++ skills later, you may skip another C++ legacy flavour in the process).
Also, if you're already fluent in another imperative programming language, it shouldn't take more than one or two weeks of a few hours after work to get an initial grip on both languages and decide whether you like them or not.
Personally, I find both languages equally frustrating for 'spare time stuff' (for different reasons though) and prefer simpler languages like C, Zig, Go, Python or Typescript (depending on the problem at hand).
I like C++ and my main caveat is that you don't just write C++. You also have to get good at tools like Valgrind and develop a deep intuition and understanding of what the language does under the hood. That's going to require a significant time investment and some people just don't have the right energy for it.
Nowadays the only reason I would learn C++ instead of Rust would be if I wanted to benefit from the much bigger C++ ecosystem, for example to build a game engine from scratch.
With C++ you'll have more offers, but you are more likely to be unsatisfied with your job: in large corps you'll usually have to untangle lots of messy legacy codes; while in start-ups things are more likely to be done quick and dirty and the flexibility C++ provides doesn't make it any better.
With Rust unless you are in the US, it's gonna be extremely difficult for you to find an opportunity; even in the US I gather there are not many chances and I _guess_ recruiters would expect higher proficiency as Rust is known for its steep learning curve.
If you have a long time horizon, Rust is the safer bet, if you want a job fast, C++ has probably more job offers.
EDIT: Ah yes, the "crawww C is unsafe, use rust crawww!" downvote crowd is out in full force today.
I’ve found that Rust requires more careful design to satisfy the borrow checker, whereas in C++ you’d just copy or use a smart pointer and move on with life. Additionally, when in Rust you have to change that careful design, the changes tend to cascade and spread.
All of the above could be largely avoided by using copying and smart pointers also in Rust, except that goes against the culture and what everyone else is doing. Most of the APIs you’re using will probably want you to use references…
To me Rust is the kind of language you’d use if you have your solution figured out and you want to implement a robust program based on that. Flexibility and development speed are not its strengths.
A warning: Rust as a language has a large surface area, and there are some features and design paradigms that can make it messy to read and write. I would treat generics as a tool to be used sparingly, vice as a default API, for example. Same with Async and Tokio.
As others have remarked, there’s a lot more C++ gigs out there. I don’t think knowing C is actually much of an advantage to learning C++ though, the idioms and libraries are too different. Personally, I like Rust, it’s got a great community and the language feels “next-gen” in a way C++ never will.
For example, one of my products embeds Chromium (via CEF), which itself is a huge C++ project. I'm interested in working with Rust, but it's simpler for me to just use C++ rather than working against the grain.
For Cloud Native stuff, Rust might be a better option, specially due to the security focus.
Just go with C++11/14/17/20 and you will be set for life.
From a practical perspective, if you're short on time and want a return on your investment, C++ will open up more opportunities on the job front.
In the future, those codebases will only be the legacy ones and you will end up doing more maintenance with C++.
With Rust your investment of today will pay off in a few years.
Deciding what to learn today is an investment decision and you should look into what you think the future will look like. To me it looks Rusty. :-)
Now, for C++ I can share some links I personally follow:
https://github.com/AnthonyCalandra/modern-cpp-features
https://www.youtube.com/c/lefticus1/videos
https://www.fluentcpp.com/
https://www.cppstories.com/
https://hackingcpp.com/
https://www.modernescpp.com/
About Rust, this link https://www.rust-lang.org/learn shares far too many goodies that any level of programmer can benefit out of it.Enjoy!
Why not? It's not like "two languages == double the time".
In this particular case, learning both side by side and comparing design choices may well lead to a better understanding and even be faster than just learning one of them.
C++11 is a well-known and popular language for software engineering, but was released in 2011. Most programs written in C++11 are being updated or rewritten in Rust because of its simpler syntax, more modern features, and generally easier development process. Additionally, many companies will hire programmers that specialize in Rust over those who specialize in C++11.
Although it is possible to find jobs doing C++11 development, we would recommend learning Rust instead.
Yes, you can get that last cycle of power from a piece of hardware by going with C or C++, but on the other hand finding people who can do that is time-consuming and expensive. And in many cases eventually not worth it.
"C++" is such a big concept toi grasp fully, that I don't see the point of studying it unless you're getting paid for it.