25 October 2021

Low-level programming

Rust vs. C++—the main differences between these popular programming languages

17 minutes reading

Rust vs. C++—the main differences between these popular programming languages

The popularity of Rust as the programming language of choice among developers is still growing. According to the Rust Survey 2020, almost 40% of surveyed developers use Rust in their work, among which 13.8% declare that they work with Rust full time. This growth might be connected with the popular yet controversial opinion that Rust can be used as a C++ replacement. However, it would be too much of a simplification to say that C++ can be easily replaced by Rust. In this article, we want to briefly present the main differences between these two programming languages and how both of them can be used. 

In this article, you will learn: 

  • about Rust and C++ features,
  • whether one is a better performer than the other, 
  • whether Rust and C++ are easy to learn,
  • in which types of projects you can use these languages. 

What is the Rust programming language? 

Rust is a child of Mozilla (born in 2010), but now it is a separate entity and has its own foundation. This multi-paradigm, high-level, general-purpose programming language is famous for its strong safe concurrency capabilities. You can easily notice similarities between Rust and C++ syntax, but Rust offers a higher level of memory safety without using a garbage collector. 

Not for the first time, Rust has been named the most loved language—it gained more than 86% of developers’ votes. But C++ doesn’t fall into oblivion and still has its supporters—it gets 43% of the votes in the abovementioned survey. Let’s check out what the C++ language has to offer.

New call-to-action

What is the C++ programming language? 

C++ was designed and released in 1985 by Danish computer scientist Bjarne Stroustrup. It is described as a high-level, general-purpose, compiled language. C++ is often appreciated for the development of applications that require high speed and concurrency. It is probably obvious, but it is worth mentioning that C++ is an extension of the C programming language, and it is often called “C with classes”. 

As mentioned before, Rust is seen as an alternative to C++, and there remains some debate in the developers’ community about in which cases it is better to use Rust or C++. Let’s compare the differences between them.

The performance levels of Rust and C++

You probably have already noticed that the Rust and C++ programming languages have some similarities. But are these programming languages as related as they initially seem? When we’re talking about Rust and C++ performance we can’t skip over the fact that these programming languages are using the same compilation model. What does that mean?

Both Rust and C++:

  • compile to native code,
  • have no runtime,
  • have no garbage collection,
  • have direct access to memory (if it is needed),
  • are low-level programming languages—they operate close to the hardware. 

For most projects, Rust’s safe mode will serve perfectly. But, in some cases, the Rust compiler can seem a bit restrictive. Then you can use Rust’s unsafe mode. This allows for using more Rust features, but there is a catch—you are responsible for the correctness of the code, the compiler doesn’t do it for you. 

In terms of performance level, there is no obvious winner between the two. There are noticeable performance differences that can be observed in specific cases, however the overall results for both languages are similar. But there is one tip: Rust could be a drop-in replacement for C++ for performance-intensive workloads. 

For more information, read more about Rust and C performance and safety

Knowing some of the C++ and Rust features you may have already guessed how and when these programming languages are used. So let’s check if you were right. 

Rust vs. C webinar

When can you use Rust or C++? 

Each below-mentioned example will work both for C++ and Rust (at least in theory). There is no technical reason which excludes one of these programming languages from a project that requires the same compilation mode and in which features influence performance level. Thus ensuring that, if you can use C or C++, Rust also should work. 

Of course, in some cases, one of them will be a better choice than the other. 

Common uses for these programming languages are:

  • software running on constrained devices/firmware (known also as embedded devices),
  • Kernel driver development—what is important is that Rust is considered an official language for Linux kernel development. C++ never reached this point,
  • libraries designed to integrate in other languages (e.g. Python), see more information on the differences between python vs rust performance.
  • backend—the Go programming language was dominant in this field, but Rust is trying its place in the Rust vs Golang battle —e.g. Dropbox used Rust to rewrite its sync engine core
  • game development—for now C++ is still the main language in this field. Rust doesn’t provide as many frameworks as C++, but its developers are constantly trying to address this, so don’t forget about Rust.

But these programming languages will not perform equally well in every case. Let’s focus on cases where we chose either Rust or C++ deliberately as the critical solution:

C++, because of its age, might seem a little out-of-date. Nothing could be further from the truth. There are still areas and uses in which this programming language remains undefeated. 

When does C++ still win?

C++ is more than 30 years old, but in some areas this programming language is still second to none. 

So how is C++ still outstanding ? 

  • existing code base in terms of existing products, 
  • compilation times,
  • existing libraries which may be available only in C++—due to C++'s long career, it has a lot of stable and feature-full libraries on its side; game development is an area where this is evident, 
  • C++ has multiple compilers: for example, Clang, GCC, MinGW, Cfront, Intel C++ Compiler, Tiny C Compiler, LLVM, and more. For now, Rust’s only compiler is LLVM-based, but a change is right around the corner
  • some language features used in high-performance code are only available with C++. But, to do Rust justice, as of May 2021, it has its own “Const generics” feature (known in C++ as “non-type template parameter”) presented by C++ enthusiasts as “Hey, but can Rust do this?”. Rust is breathing down C++’s neck with its improvements, and it isn’t going to stop. 

Finally, a small bonus. It is easier to find a job as a C++ developer than a Rust developer because there are still more products being written in C++

Performance and use cases aren’t all—let’s talk about how these programming languages are to learn. 

>> Explore our Rust development services

Learnability

When you are considering learning a new programming language, either you or your team, you need to know what to expect before you begin. 

This graphic comparison of the process of learning each language could be helpful (but not necessarily reassuring). You can think of the learning curve as a mountain when you start with C++, and as a wall in the case of Rust. (But when you punch through the Rust wall, you will probably notice that it is not as bad as it seems). 

Why should you prepare for a mental challenge (despite your experience as a developer): 

  • Rust requires learning some concepts up-front. C++ may be more forgiving to beginners.
  • Rust features unique concepts not present in other programming languages—like ownership and borrowing.
  • Rust often requires unlearning many patterns from other languages, which could make it easier to learn for the less experienced developer with less ‘baggage’ to shed. 

But there are some aspects you should keep in mind regardless of which of the two languages you choose to learn: 

  • Treat the compiler as a friend—this  talk by Esteban Kuber is a Rust tutorial on how to deal with the curve, 
  • C++ is easier to compile, but harder to make a correct/efficient program,
  • C++ contains a lot of “footguns”—features that are likely to be used in a way that will cause problems for you later. 

The beginning with both languages could be challenging despite the developer’s experience. But maybe a high safety level can pay off the effort?

Rust and C++—how does the safety of these programming languages compare? 

These two programming languages have many similarities. But there is one thing which definitely divides them: safety. Rust is a more secure option than C++—a potential error causes code rejection. Rust is “safe-by-default”. It balances preventing undesirable behaviors and allowing for them if the loss will not be too great. In C++, an error might slip through the review process and cause application crashes or even security vulnerabilities. 

In C, you publish your api if it's possible to use it correctly (open world). In Rust, you publish a safe api if it's impossible to use incorrectly (closed world).” This quotation clearly shows the difference in using these two programming languages (it refers to the C programming language, but there is no difference with C++ in this case). 

When we are talking about safety, Rust leaves C++ far behind. Being one of the reasons why companies are using Rust.

Tooling is another aspect that influences the developers’ work quality—how does it look in the Rust and C++ cases? 

Tooling in Rust and C++ 

Tools should facilitate creating, debugging, maintaining, and supporting programs and applications. Do these programming languages provide similar solutions? Let’s compare them. 

  • Rust offers friendly, out-of-the-box tooling. Cargo is a single standardized tool for Rust’s package dependencies, compiling packages, making distributable packages, and uploading them to crates.io. Thanks to this, every Rust code repository looks similar—you will not be surprised by the dependencies, repository structure, when the tests are, and more. In the C++ repository, on the other hand, everything looks different and you have to learn the repository from scratch every time. 

  • C++ has no standardized tooling, but some things are more available due to the maturity of the language:

    • Debuggers—GNU Debugger, Visual Studio Code, LLDB, Nemiver. These are only a few examples of the most popular debuggers for C++. 
    • Commercial static analyzers—Cppcheck, Clang Static Analyzer, sonarqube, Sourcetrail, and more. 

Good tooling sometimes is not enough if you’re dealing with unexpected difficulties—then you need support from the community—people who know the programming language inside out who are willing to help. 

Community

Behind every language stands its community—it cares about language development, keeping libraries up-to-date, and coming forward with proposed improvements. Put simply, the community works on the language’s development. How do the Rust and C++ communities compare? 

Both programming languages can proudly say that they have a great community. The Rust and C++ communities are both friendly and very active. Whatever the issue, you can easily find a bunch of forums with people eager to help. You don’t have to take our word for it, find out by yourself by checking out these pages: Rust Community and C++ page

Moreover, both languages have their official annual conferences:

Rust, in comparison to C++, is much younger, but it has an equally active community. But in this case, C++’s age matters. C++ has more forums, open source projects, and to be honest, developers have already met and resolved most of the possible C++ issues over the years. 

Are we convincing you to switch from C++ to Rust? Read on to learn if it is possible and how to do it. 

How to switch between Rust and C++?

First, do not worry—Rust-C++ integration is possible. You can ‘Rustify’ your code piece by piece! The question is: how to do it right? 

In 2020, CXX appeared, and it was a real revolution. Thanks to the CXX library, calling C++ code from Rust and Rust code from C++ is safe and effective now. It does not mean that it was impossible before. It was, but before CXX, Rust-C++ integration was more inconvenient—it required manually crafting Rust-C and C-C++ interfaces. 

Not impressed? Meet Autocxx—the boosted, automated version of CXX. Autocxx is not an officially supported Google product, but the company does use it—interesting! 

Conclusion

Both C++ and Rust are potentially excellent choices for your next project—with both having great performance, tooling, and community support. There is no obvious winner, but there is never a one-size-fits-all solution when we are talking about programming languages. If you aren’t already up to speed on Rust, we recommend you catch up. The Rust programming language is constantly gaining in popularity because of its focus on safety. One thing is clear, when it comes to this feature, Rust beats C++ hands down.

Michał

Michał Krasnoborski

Software Engineer
Bartosz

Bartosz Różański

Senior Software Engineer