Is Rust an alternative to C++? The question is frequently asked in programming forums and blogs. In fact, Rust and C++ have several common points. However, even if the goal is the same, the syntax and features between the two languages are different. Let's review them!
Is Rust an alternative to C++? Why this question?
Let me tell you a story: This week I done two things:
- First, I learned a new programming language! Rust. I think I'll talk to you about this language in a future post.
- Secondly, during a review, I saw a terrible kind of code which resulted to a crash (fortunately!)
This code concerned two vectors which managed the same pointer (with value created in the Heap). If I can summarize the behavior, this error can be summarized to something like this:
Little code review: Vectors with "shared" pointer
First, two vectors of pointers are created.
Secondly, the pointer is allocated (through my method "createIntInHeap").
Thirdly, this pointer is pushed in the two vectors.
Fourth, I delete the concerned pointer by using the first vector.
At last, I try to use the pointer in the second vector.
Output, behavior and error.
This kind of code cannot work correctly... Here the output:
What happened here? On "push_back" call, a vector will create a new object. In our case, this is a pointer of int which point to another pointer of int.
You got the point! We can resume this design defect with this picture:
By deleting the value through vector1, it didn't affect the object contained in vector2. This kind of error is a classic in C++.
We are talking about C++ and Rust, why this anecdote?
I reviewed this anecdote to highlight one of the great advantages of Rust: The Security. Indeed, Rust, thanks to the ownership principle, guarantees a protection against this kind of "shared" value. As well, This protection is directly done in compilation time. In C++, we can only detect those errors during code review or runtime. The Rust documentation highlight this point:
Actually, those errors concerning the pointers and dynamic allocation are extremely frequent in C++. Rust had been designed to avoid those errors.
Why Rust and C++ are regularly compared?
The response is simple : Because they are both "System programming language". It means they provide both equivalent performances and direct access to the hardware of the machine.
In addition, Microsoft announced recently to migrate a part of the Windows OS system to Rust:
https://www.youtube.com/watch?v=NQBVUjdkLAA
Likewise, compared to C++, Rust had been designed to catch easily errors. So, if we compare the both:
- Rust has (approximately) the same performances compared to C++.
- It is safer than C++.
- In addition, it has a great error management system.
- Moreover, Rust has a packaging manager included (Crate)
- ...
Ok Axel, Rust is performant and Safer. Then, why are we still using an unsafe language as C++?
In my Opinion, Rust has several disadvantages compared to C++:
Rust is still a young language.
That's right, Rust had been announced in 2010 and the first version of the language had been released in 2015.
Compared to the C++ (First version in 1983), it is young!
Consequences: C++ is more complete concerning the libraries. There are many packages for testing, framework for video and other stuff.
The gaming industry.
Certainly linked to the first reason. In the gaming industry, the principal language used is the C++.
Indeed, many frameworks as the Unreal Engine are developed in C++ to guarant performances.
As Rust is still young, such framework and gaming engine still not exist. It is a whole part of the industry which cannot use Rust.
In fact, Rust cannot be an alternative to C++ in those domains.
Difficulty to learn.
Even if Rust is regularly compared to C++, It borrows syntaxes and principles from other languages like Python and JavaScript. Besides, the syntax is different and the ownership principal/multithreading are concepts hard to assimilate.
Maybe, Rust is principally adopted by developers which already develop safe C ++ code.
Yet, a compiled program in Rust has more chances to correctly work compared to C++. Even if Rust is harder to learn than C++, the advantage of the language is still the "safety".
So, if those issues are fixed, Rust will definitely replace C++ one day?
Technically, it could be the case. If Rust overcomes those default, it can replace the C++.
However, the reality is not like this. Rewriting a billion of lines of C++ code and training current C++ developers are extremely expansive.
Secondly, Rust is not yet a standard in the industry. Even if the language is innovate, it cannot replace a language with such history as the C++.
Conclusion
Rust is indeed a great alternative to C++.
Don't think that I hate C++. In fact, I love C++, it is certainly one of my favorite programming languages:
It is Performant. There is a great choice of library. And the new standard like C++11, C++14 and, in the future, C++20 are great!
But, the fact is that C/C++ had been developed a time when safety issues wasn't common.
Even if C++ looks easy to learn at the first sight, it is easy to implement a security error.
Actually, There is many young developers who ignore those principles and implement a no-safe code. Today, Rust is not frequently used in the industry. To avoid security issues. The best is to continue to help junior developers to develop safe code. No stupid and nasty blame, simply formation and help.
It is easy to be proactive in C++, although, mastery this language is still a long way.