The old warrior stood, scarred but unbroken. The young challenger raised his sword, burning with ambition. Yet the elder’s eyes whispered a truth: strength forged over decades does not fall easily to youthful fire.
Since the beginning of time (i.e. creation of C), the world has introduced thousands of warriors to challenge, defeat, and replace C and his mighty son, C++. Yet no one really succeeded, except for who did not win, but at least, survived. The name is Rust, a language that tries to be safe by default, unlike C/C++.
Rustaceans (I personally call them rust-people
), have taken this fight so seriously that things are getting a bit out of control!
But, regardless of all my love for C/C++ and the point that I'm not much of a Rust fan, I'm going to do an unbiased realistic comparison between the two greatest warriors of the lands of system-level programming.
Let's begin with an introduction of each.
What a System-Level Programming Language is
A system programming language (also called a low-level programming language) is, as the name suggests, a programming language used for system programming. I'm not going to explain it in detail here, just a brief introduction.
A system programming language gives you the ability to speak directly to hardware, allocate and free the memory as you need, talk to your graphic card and ask it to render a game in full resolution for you. C, C++, and Rust are considered low-level programming languages.
On the other side of the table, we have high-level programming languages. While using these, you don't have to worry about memory as you do not have access to hardware anymore. These languages use tools like garbage collectors (GC) to manage memory and prevent risks. Python, Ruby, etc. are examples for high-level languages.
Enough of this, let's talk about the Titans.
C/C++: The Beginning of Time
In 1970s, a successor to B programming language was created by Dennis Ritchie. This great successor was C programming language, a low-level language that is still widely used today for programming kernels, drivers, operating systems, other programming languages, and many more things. We can say that many programming languages we have today are, directly or indirectly, created by C.
In 1985, Bjarne Stroustrup introduced C++ as an extension of C, adding object-oriented (OOP) features to C. Unlike C (which is simple and has a few keywords), C++ is a very, very huge language with so many abilities and is getting bigger every year (beginners consider it a nightmare!). C++ is so powerful that you can do anything you want with it. let's take a little look at pros and cons.
Pros
- C/C++ are mature old languages, with thousands (if not millions) of libraries and frameworks.
- Amazing for designing GUI applications, thanks to Qt, Dear ImGui, and many more.
- Strong and huge communities, solutions can be found easily.
- Lots of learning resources.
Cons
- Most important: Not being memory safe by default. Safety relies entirely on you.
- C++ has a steep learning curve due to vastness.
Let's talk about Rust.
Rust: The New Warrior on the Field
In 2015, Mozilla Foundation introduced Rust, a programming language designed with performance and safety in mind.
When it comes to memory, there are basically two points of view: a) relying entirely on the programmer and/or b) using a garbage collector. Rust offers a third view: Ownership. By the help of ownership system, your code is memory safe without relying on you or using a GC, and that's amazing! Let's analyse it in more detail:
Pros
- Rust implements best practices as it's default, so it's safe.
An example I can give you for this, is that a good way of keeping your program safe in C++ is by using constants instead of variable, unless you need to change the value:
const int number {12}; // instead of int number {12};
But Rust is different. In Rust every variable is immutable by default, and if you need to change the value later, you must explicitly declare that using
mut
keyword:let number: u32 = 12; // immutable let mut number2: u32 = 12; // mutable
- Uses
cargo
package manager, and makes it much easier to manage dependencies - Thanks to the borrow-checker and ownership system, your code is almost always safe (I'll tell you how it can be unsafe in the next section)
Cons
- Rust is still young, and therefore it doesn't have a huge community like C/C++ (though the community is growing bigger day by day).
- Still not good for GUI applications due to the small community and lack of native libraries.
- Does not have as much libraries and frameworks as C/C++ have.
- Steep learning curve due to complications caused by ownership system and borrow-checker.
- Slower compile time due to compiler's borrow-checking and making sure of safety.
- Coding in Rust can take longer as you need to write more code due to ownership rules.
- By default, no direct access to hardware is given, but access can be achieved using
unsafe
. - Can become unsafe by the developer. See the next section.
Direct Hardware Access
When it comes to low-level programming languages, direct hardware access is essential, otherwise low-level programming will be meaningless.
C/C++ provide this access, but Rust doesn't. Rust does not trust you with memory and hardware as you might cause huge problems with such power!
Still, Rust knows the need for hardware access, so it provides you with unsafe
. In Rust, you can declare an unsafe code block in with you can access memory without any limits, this way your program has only one dangerous part which is only implemented if direct access is necessary (most of the times it isn't).
So far you can see that Rust is much safer to use than C/C++, so why earlier on this article I said I don't like it? Let's find out!
The Rustacean War: A Rewrite Massacre
Since the creation of Rust, the idea of moving from C/C++ to Rust for better safety has become popular and more people are doing so. An example is implementation of Rust code in Linux Kernel (which is entirely written in C).
I don't believe this is a bad thing. Actually I sometimes support this idea as it tries to make the world of code safer. The biggest problem here is that some Rustaceans are so focused on proving that Rust is better than C/C++ that all they do is rewriting every already-existing safe-coded useful programs (regardless of the language their written in) with Rust! Projects like sudo-rs
as a replacement of the good old sudo
(that has been around for years and has never had a memory problem!) are a good example of this.
Rust can be used for amazing projects, kitty
terminal, mdbook
, zed
IDE, etc. are examples of creative Rust code.
This is why some people (I myself included) cannot accept Rust. Because programming is about creativity, not trying to prove others that your language is better.
Create, don't re-invent the wheel!
Real Safety and Security
Matters of safety and security, though seem alike, are quite different (I'll write about them in detail in another article). Language is a tool like a surgery knife, it can take your life or it can give it to you. The most important element, the key element of a safe and secure program is you, the programmer.
Rust tries to be safe by default, but you can turn it into a mess. C/C++ is unsafe by default, yet you can write it safe. If you want safety of your program, read and learn. Remember, you're all that matters, safety and security depend on you.
Final Words
Code with the language you like, do not ever listen to others telling you that it's not a good language. If you master it, then it's a good language in your hands.
Be creative, don't just rewrite other tools!
And enjoy coding.
Create. Love. Improve.
- A. Aryani
Top comments (0)