C++ v. Rust -Speed, Safety, & Community Comparison

Development

2021-09-17T18:24:53.490Z

Zigzag

Rust is the new and exciting language on the block, while C++ has been around for decades. With so many technical teams comparing rust vs C++, you’re probably curious. Is one better than the other? Which one should you use?

Below, we’ll be comparing C++ with Rust, sharing our thoughts on when you should use each language, and predicting the future of C++ vs Rust.

Let’s start with the basics.

A Bit About C++

C++ is a programming language that’s been around for decades. It’s used in a lot of embedded tech and systems that run apps -- not necessarily apps themselves.

One of the pros of C++ is that it’s a low-overhead language, which means it doesn’t take much time to compute. This makes C++ incredibly fast—and valuable for programs that require heavy processing (music streaming) or low energy (heart monitors).

However, C++ certainly has its downsides. C++ is an old language lacking many of the features modern languages have. Simultaneously, C++ carries the cruft of decades of design by committee.

Some pretty successful software companies use C++, including Adobe Photoshop, Microsoft Office, and YouTube.

A Bit About Rust

Rust is a relatively new language, created in 2010. It was essentially built to focus on a lot of the problems languages like C++ have e.g. handling pointers and null.

Therefore it touts similar benefits -- a low overhead language built for speed and processing. Except the biggest difference is that it’s exceptionally easier to read, write, and change.

Software that uses Rust includes Parcel (js build system), Redox (a Unix-like OS), Firefox engine, and Ruffle (in-browser flash emulator). Arguably the biggest example is Figma, who rewrote their multi-edit logic and got huge performance gains. They go into more details here.

Here are more cool things you can do with Rust.

What are the Similarities?

Before we dive into the differences, here are the basic similarities between C++ and Rust.

  • They can both compile to static binaries and require minimal runtime dependencies
  • Both are good choices for embedded development because of the overhead
  • Both are fast
  • Project structure and organization are similar

C++ vs. Rust - a Developer’s Comparison

Hero quote

Writing dangerous code in Rust is always a conscious decision.

C++ vs. Rust Speed

As far as speed/performance, Rust is on the same page as C++. There are situations where it is easier to write faster programs in C++ because it's easy to ignore fundamental problems in the programs.

When pitted against each other in unbiased benchmarking, Rust has beaten C++ in many instances. Here are some examples from The Computer Language Benchmarks Game. They include:

Reverse-complement

  • In Rust: .45 seconds
  • In C++: .86 seconds

Binary-trees

  • In Rust: 1.02 seconds
  • In C++: 1.54 seconds

Fasta

  • In Rust: .76 seconds
  • In C++: .78 seconds

N-body

  • In Rust:  3.42 seconds
  • In C++: 2.18 seconds

From this small sample size, it’s clear that both are fast. Sometimes Rust is a bit faster, and sometimes C++ takes it.

Compile times:  C++ is faster but as you opt in to certain safety features, it starts slowing down. The compilation speeds are worth the additional time to avoid hard to track down issues later.

So which one’s better? Both Rust and C++ are fast. While in some scenarios one beats the other, you probably won’t choose one over the other for speed alone.

C++ vs. Rust Safety

There are flags for C++ to enable a lot of the same protections that Rust has, but they're not enabled by default.  Conversely, Rust has the concept of unsafe code for encapsulating undefined behavior. Thus, while C++ has safety options that are opt-in, Rust is always opt-out, meaning that writing dangerous code in Rust is always a conscious decision.

Rust doesn't have null. Null References: The Billion Dollar Mistake

Recently, Microsoft praised Rust as “the best choice for the industry to adopt whenever possible due to its ability to write systems-level programs in a memory-safe way."

So which one’s better? In general, it's much easier to be confident that Rust code is safe, especially for junior developers.

C++ vs. Rust - Ease of Use

Without a doubt, Rust’s ease of use dunks on C++. Between resources for getting started, library support and integration, and the developer community, we believe Rust is in a good position to take over C++ unless C++ does something drastic to improve.

For beginning developers, Rust has excellent documentation. This Rust book makes it easy to get started. This is the polar opposite of C++, where there are a variety of different 'dialects' that developers have to learn and only senior developers can excel.

​Using `cargo` with Rust makes it easy to include libraries and build to different systems, something C++ never codified. Overall, the project structure in Rust is more consistent and libraries are far easier to use.

Finally, developers simply love Rust. According to the Stack Overflow Developer Survey, Rust was the number one “most-loved” language.

C++? On the “Most-dreaded” list. Our team tends to agree.

So which one’s better? There is no question that developers enjoy writing in Rust over C++.

Okay, Which Language Should You Use?

If you’re starting from scratch, it’s going to be much easier to build a safe system without needing to hire senior C++ developers. It’s also helpful to rely on Rust's build system to avoid reinventing the wheel.

If you’re building something embedded, it's easier to be safe with Rust, though you will probably need to rely heavily on unsafe code, since the performance requirements are more intense.  Rust can also readily interact with the C ABIs which means that it can readily use any existing C code for embedded development.

If you’re building a GUI, C++ libraries are far more developed and mature than Rust. In this scenario, it's going to be easier to use C++, since more of the groundwork has been covered.

Future Outlook - Rust vs. C++

Will Rust overtake C++?

We love Rust and hope it does replace most of C++’s use cases. Because it’s so much easier to develop, it saves time, money, and energy for developers and companies who need certain product development.

Of course, if C++ updates to compete with Rust (which would be difficult for them to include all the memory safety without significant bumps to compile-time), then we will reevaluate. But at this moment, we are excited to see how Rust makes development easier for product teams.

Want to work with Devetry? Our software developers use Rust, C++, and just about any other programming language.