C++ is a venerable programming language that is over thirty-six years old. Can newcomer Rust match its versatility and speed?
C++ is a venerable programming language with over 36 years of its long history. It's a staple in the programming landscape, and most people, even those who don't work or aren't interested in technology, have probably heard of it at one time or another.
So can newcomer Rust, released in early 2010, match the versatility, speed and other qualities of C++, its much older counterpart? In contrast to C++, Rust is not as well known. But, of course, that could very well change.
Today, there are many Rust developers who trust the language, just as there are many programmers who trust C++. And the two languages are often compared, thanks to certain overlapping features they both possess. Here, we will evaluate the key features that Rust and C++ offer and present a clearer picture of Rust vs C++ in general, especially in terms of the performance each can provide.
Aspect | C++ | Rust |
Memory Security | Prone to memory safety issues (e.g. buffer overflows) | Designed with memory safety in mind, prevents common errors through ownership model |
Concurrency | Offers manual control but can be prone to errors | Provides safer concurrency primitives, making concurrent programming more reliable |
Runtime performance | Highly optimized, with control over low-level details | Comparable to C++, slightly overloaded due to security checks |
Compilation speed | Varies, but can be slow for large projects | Generally slower due to more complex build checks for security |
Error handling | Exception-based can lead to performance overhead | Uses the Result/Option pattern, which can be more predictable in terms of performance |
Standard Library | Extensive, with many legacy pieces | Modern and minimalist, with a focus on security and performance |
Use case flexibility | Widely used in various domains including systems and gaming programming | Increasingly used for systems programming, web assembly and embedded systems |
C++ in a nutshell
First, let's take a look at C++ and how and why developers use it today. And no discussion of C++ is complete without mentioning C and the C family in general.
C and its object-oriented cousin C++ are two of the most well-known and venerable programming languages on the market. When speed is the main factor in software development or when resource management is an issue, these two languages are still the default choice, even decades after their creation. In fact, C dates back even further than C++, its offshoot, to 1978.
Every few years, an individual or group of developers will attempt to build and release what have come to be known as “C killers,” programming languages designed from the ground up to provide the same benefits as the C family, while trying to account for some of the problems most known associates of the C family at the same time. These would-be C-killers generally have mixed success.
Just as an example, we have Java and C#, both of which are amazing and have built great resources and communities. And if you take the mobile market into account, Java has probably become the most used programming language in the world.
Rust in a nutshell
Rust, as we noted, is a much newer language. It was presented as a powerful, multifunctional and very fast programming language focused on both security and performance. Rust is an example of one of the newer languages that has been considered a “C-killer”.
Unfortunately for those eager to see the C family disappear forever, it doesn't look like Rust will necessarily be the one, although it has still emerged as an excellent and useful language in its own right.
Additionally, Rust does a lot of things right and is an amazing alternative for people looking for powerful programming languages with modern sensibilities. We will see some of its capabilities and qualities below.
A quick note
Before we go any further, a warning: here, when evaluating these two languages, we are not just thinking about speed. Objectively comparing the speed of languages is difficult as there are many variables involved, from how each language handles certain tasks to the experience and inventiveness of the developer writing the code. However, we can use some metrics to compare speed and performance impartially. We will clarify this a little further below.
A quick example: Python is considered one of the slowest languages on the market, but an advanced Python developer can create programs faster than a person who is working in C for the first time.
It's important to keep this in mind when reviewing the section on benchmarking information and the table we've provided below. Unbiased benchmarking metrics, although difficult to evaluate when you compare Rust code and C++ code, provide a clearer picture of overall speed and performance.
Now that we've got that out of the way, let's dive into Rust's features and compare them to C++ to determine which language is best for your project.
Rust vs C++ performance benchmarks
Although it is quite difficult to evaluate the performance of the Rust and C++ language, it is possible and it starts by looking at the source code. Here is an excellent resource to better understand the comparison between C++ and Rust in terms of performance benchmarks, and you can take a closer look at the table below to understand them better.
In short, although Rust code and C++ code are comparable in terms of speed and overall performance, Rust generally outperforms C++ in several cases when we consider unbiased benchmarking.
High level and low level
Both C++ and Rust are considered “low-level” languages, or at least lower-level than other popular high-level languages like Javascript or the aforementioned Python. But what exactly does this mean?
Computers follow arithmetic and logical instructions to perform whatever task they are programmed to do. For example, to display the text you are now reading, your CPU sends a signal through electrical currents giving instructions about the color of each pixel, creating the image you are seeing.
A computer doesn't know what the letter A is, but by following mathematical instructions it can draw it on a screen. These instructions are called machine code. On the other end of the spectrum, you have natural language, the way we humans speak, read, write, and communicate with each other.
A programming language basically serves as an intermediary between machine code and human language. It is the way we communicate with machines and, therefore, it can be more or less difficult to translate and understand. Developers are essentially giving machines sets of instructions on how to act and behave through programming languages — but we don't have the same vocabulary, which is why these languages are needed.
In short then: when someone says that a language is low level, what they are saying is that it is closer to machine code than to the way we speak and communicate.
The higher the level of a language, the more it resembles human language, but at the same time the more processing power is needed to transform this program into machine code. This is why languages like Python are very readable, but they are also slow and poorly optimized.
Suffice to say, Rust and C++ meet a very similar need, namely code that is readable but can run fast enough for heavy software like operating systems or drivers.
Rust: safety first
Rust was created in 2010 by the Mozilla Foundation. It started as a side project for one of the developers that grew quickly as the foundation realized the potential it had for developing its software. This was an assessment that proved true, as in its short lifespan, Rust became one of the most beloved languages among developers.
One of the main reasons this happened is that Rust has two killer features that set it apart from its competitors: safe concurrency and memory safety.
Concurrency is the ability of a program or software to execute several of its parts out of order and/or in partial order, which in turn means that you can have parallel execution of simultaneous processes.
Let's say you have a program with 10 instructions. Instead of executing each one at a time, you can have multiple processors executing multiple instructions simultaneously, achieving the same result in less time.
While other languages leave threading to the developer, manual threading requires a level of knowledge that not every developer has. Rust checks property statically to ensure that a developer is not inadvertently creating a bug by causing the program to access information when it should not.
The same goes for memory management. Typically, memory is manipulated directly by the developer or left for what is known as garbage collection, that is, letting the computer figure out what information is no longer being used and freeing memory.
While garbage collection is awesome, it can be quite slow and expert developers often find it restrictive. Instead of GC, Rust avoids null pointers, dangling pointers, or data races, all along with safe code. Fewer bugs mean faster development times.
Simply put, Rust is like driving a race car with a seatbelt: it's a lifesaver for someone just learning to drive, and it's a good safety measure for an experienced driver, even if he's unlikely to crash. That's what memory security is all about.
C++: the tinker's dream
C++ is 36 years old, and in that time it has accumulated thousands upon thousands of libraries as well as a knowledge base that is simply baffling. No matter how crazy or far-fetched your idea is, chances are someone has already done something similar in C++.
Furthermore, C++ is a tinkerer's dream come true. Few languages offer as much freedom to the developer as the C family. Like a finely tuned Stradivarius, it is a tool that in the hands of a maestro can truly create a work of art.
Do you have a Windows operating system? You are using C++. Do you like YouTube? This is the language that handles video processing. Have you ever played a game made in the Unreal engine? This is also running in C++. There is no better poster child for the concept of multipurpose language.
As a company looking for developers, the talent pool for C++ programmers is a thousand times larger than Rust, at least for now. Who knows what distribution will be like in 10 years?
More libraries mean less development time, as there are more tools available for developers to use freely, and in short, there is a lot of evidence that C++ is still the fastest object-oriented language available.
C++ vs. Rust – Ease of use
There is no doubt that Rust is much easier to use than C++. It also has a significantly lower learning curve, along with extensive community support, libraries, tools, documentation, and additional resources that Rust newcomers can take advantage of when learning the language.
In contrast to the Rust language, C++ is widely considered a difficult and complex language to get to grips with, with only very experienced developers turning to the language frequently. For one, C++ includes a multitude of diverse dialects that developers need to learn before they can fully utilize C++.
Also keep in mind that Rust often ranks as one of the world's most loved languages in Stack Overflow's annual developer survey, while C++ often makes the most feared list. This shouldn't come as a surprise to developers, many of whom have abandoned C++ in recent years, often in favor of Rust as a better alternative.
The future of rust and C++
As you can see, there are many pros and cons to using Rust and C++. And you may be wondering: what will the future of these two somewhat similar languages be like?
The fact is, of course, none of us have that answer. C++ surpasses Rust in terms of longevity, but as we know very well, it's not that difficult for languages to lose popularity and even be retired entirely. Even so, C++ continues to enjoy broad community support.
Meanwhile, Rust brings many advantages such as ease of use and low learning curve. This means that it is more likely to attract a greater number of programmers, especially those who are new to the field, in the coming years. It also shines in the case of memory consumption and has fewer memory safety issues. That being said, it has a much smaller community, perhaps due to its young age. And, of course, this could very well change in the future.
So can we predict what will happen with Rust and C++? It seems unlikely that any of them will disappear in the near future. C++, along with its C family of languages, remains a staple in the programming space and is even the first choice that developers turn to when working on certain types of projects.
And although Rust is a newer language, it is only gaining popularity. In 2022, it was crowned the most loved language in the Stack Overflow Developer Survey , marking its seventh consecutive year in this position. So it's also clear that Rust is on the rise.
In short, we can expect long and illustrious paths for these two important languages.
Which one to choose?
This comparison is quite confusing and it would be a disservice to point to one language and declare it the winner. Fortunately, there doesn't need to be one: Rust and C++ are very similar and integration between them is possible. Both languages have proven to be invaluable assets in software development.
If your project requires speed, then you can't go wrong with either option – it's simply a preference between the security of Rust or the customization of C++, along with other features that come with both options.
What to consider
That said, we've reviewed the strengths and limitations of both languages, and you can judge for yourself which one is the best choice for your project or projects. After all, as you've probably already noticed, developers apply Rust and C++ to different types of work and in different ways, although there is often some overlap in terms of which projects are appropriate for the two.
So when you think about the best choice between Rust vs C++, think about features like:
- The experience and seniority of your developers or developer bases that you can leverage
- The importance of high-performance code for your project
- Memory Security and Memory Management
- Community support and available resources
- Your current batteries
- The operating systems you want your program or system to run on
- The necessary functions you need to perform
- The types and complexity of projects you typically work on in your organization
- Any other factors you consider important to you and your business
These, of course, are just a few of the many factors that will influence your decision. It really depends on the unique product or project you are creating. Suffice to say, there are some cases where both C++ and Rust will appear in a company's stacks, especially when they have a large and varied product catalog and arsenal.
Rust vs C++ FAQ
Is C++ faster than Rust?
C++ is not necessarily faster than Rust. It's difficult to directly compare the two languages in terms of speed and performance. Generally speaking, Rust and C++ are comparable in terms of speed and overall performance, but when we take unbiased benchmarking into account, there are many cases where Rust will perform even better than its counterpart.
Why is Rust slower than C++?
Many developers believe that Rust is slower than C++, but this is not necessarily true. At first glance, it may seem like Rust is slower than C++, but that's only when you look at the big picture, not the unbiased benchmarking data that is essential to consider when directly comparing Rust to C++ in terms of performance.
In fact, although developers can sometimes write and run C++ programs faster than Rust programs, in doing so they are ignoring many of the language's fundamental errors, which will likely lead to more extensive problems in the future.
Is Rust easier to learn than C++?
Rust is widely considered easier to learn than C++. C++ is notoriously difficult, with experienced and senior developers mostly turning to it. Meanwhile, Rust is believed to have a low learning curve. It's also easy to use and has several features to help developers who are new to the language get started. That said, C++ has a large support community available for assistance when needed.
Is rust growing in popularity?
The popularity of rust is growing every year. In 2022, it marked its seventh consecutive year as the most loved language in Stack Overflow's annual developer survey, with 87% of respondents saying they planned to continue using it. Meanwhile, it tied with Python as the most searched for technology in the same survey. So it's fair to say that Rust is already popular and continues to rise.