C, C++, Rust, Python, and Carbon (When to use Which?)

Ugur Yagmur
CodeX
Published in
7 min readOct 18, 2022

Before digging into the details of this topic, I strongly recommend you read my previous article about programming language comparison. Because we will use the mentality provided in that article to decide between those languages.

If you didn’t read the other article, I want to inform you about myself. I focus on performance-critical software and as a result, I’m generally using compiled languages like C, C++, and Rust. Also, for scripting, I’m using Python. Since I don’t have enough expertise in other programming languages, if you’re interested in the others, you should research them by yourself or you should find other articles.

Let’s dig in and analyze the pros and cons of the languages we will consider.

Python

Python is a scripting language. It’s very simple, and lean, and there are lots of libraries in them to do whatever you want. The package managers pip and conda are very powerful and the automatic virtual environment creation system works very well to run python software by using different specific versions of other libraries.

On the other hand, Python is an interpreted language. The types created in python are always allocated in heap memory and there is a garbage collector to prevent memory leaks. In addition, for the multithreading approach, there is a Global Interpreter Lock (GIL) mechanism which is very unhelpful in increasing the computing performance. However, you can use it to optimize I/O queues. Thus, the performance of Python is not that good when considering the others.

C

C is a masterpiece. It’s very lean and simple. If you read the previous article I wrote, it’s the best energy and performance-friendly programming language. The optimization of the language is very good and Linux is written mostly in C because of these reasons. To be honest, I’m a big fan of this language, and generally, if there is a performance need in a project, people prefer using C. For instance, VideoLAN video encoder projects are implemented mostly by using C. Similarly, OpenGL, GIMP, and some other famous software are written in C.

However, like everything else, there are drawbacks to C. If you want object-oriented features, there is none in C. It’s not type or memory safe, so you need to know what you’re doing; otherwise, the software you develop will cause lots of problems. In addition, I think the biggest missing part in C is the generic types. If you want to design an algorithm for different types, generally you need to define them separately. Also, there is no automatic compilation system or package manager. You need to create makefiles which are basically recipes to compile your software.

Rust

Rust is a relatively new language. It was released in 2010. The main philosophy behind the language is forcing developers to follow certain rules to make software written in rust memory-safe, type-safe, thread-safe, and null-safe. The compiler is written in a very good way and it gives good suggestions to fix your code. (However, it drives you crazy until get used to it). There are generics in Rust and traits — which are interfaces basically — which are very helpful to design very useful libraries. The energy and performance efficiency scores are very close to C, which is amazing for a memory-safe language. In addition, it has a built-in package and dependency manager, which is called Cargo.

What are the drawbacks then? It looks like there are no drawbacks since its performance is close to C and lots of security issues while providing generic and trait functionalities with a built-in package manager. There are of course drawbacks, for instance, there is a discussion if Rust is an object-oriented language but it’s not. The object relation mechanisms like inheritance are not introduced in Rust. On the other hand, in some memory-related operations, Rust uses lots of memory according to its competitors which makes it a bad choice, especially for embedded software development. In addition, lots of libraries are written in C and C++ so if you want to use a library used in these languages in C, there is a C API, however, since the compiler mechanisms are very different, sometimes it causes problems. Another drawback is that although the language provides memory safety protections, it’s still possible the cause memory leaks.

C++

C++ is basically C with object-oriented features. It can do whatever C can and almost everything Rust can — yes it can implement similar ownership and borrow mechanisms like Rust but it’s not forced by the compiler. — Also, C++ has a very strong standard library that contains smart pointers to provide memory safety, and containers to store data efficiently. It has a template mechanism that is very similar to generics and helpful to develop high-quality software.

However, C++ has too many functionalities which might be confusing from time to time. It may cause segmentation faults and memory corruption if you don’t know what you’re doing. There are lots of details and there is no built-in package manager. In addition, because of the mechanisms and functionalities it provides, the performance of the software written in C++ is a little bit lower and the energy consumption is a little bit higher when compared to its competitors.

When to use Which?

Although the performance of Python is significantly lower than the others, it’s very simple and rapid to test your ideas and algorithms. Thus, if you need to test your idea, and implement a state of proof of concept software, Python might be the best choice to start with.

After implementing your idea in Python, if you need performance, you can convert performance-critical parts to C and since there is a C API in Python, it will be very easy to use them together. In fact, there is a library called Cython which is used to convert Python code to C code. However, it’s not that optimized, so if you need a very optimized code, then the best way to do it is to write in C by yourself. You can optimize your software by writing it in C, C++, or Rust.

If you don’t need C libraries or if you want to force your teammates to follow certain ownership-borrow rules to keep the system safe, then Rust might be the best choice. The performance of your software will be close to C while making it also memory, thread, type, and null safe. Also, Amazon and Facebook declared that they started using Rust to develop company tools, which means we might see more Rust in the future.

Both C and Rust are very optimized and good languages. However, if you will manage and maintain a very big project with a big team or as an open source, then object-oriented features might be very helpful. If you also need performance, then your language is C++.

Briefly, you should choose what you need most. Most companies are following a very similar approach for the systems they develop. For instance, DeepMind bought a library called MuJoCo which is used for physical simulations for reinforcement learning. The interface of this language is written in Python since they need to combine it with other deep learning libraries to test their artificial intelligence ideas in a fast way. However, performance-critical parts were written in C at the beginning, and as the project gets bigger, they added C++ to maintain and manage the codebase in a better way. The same thing is applied to performance-critical Python libraries like NumPy, Tensorflow, and PyTorch. On the other hand, in the open source community, a library called RipGrep is implemented which is a text-searching software in file systems. It’s very useful and since it doesn’t need to interact with other libraries, it’s written in Rust and its performance is quite impressive. As you can understand from these examples, the important thing is not being a fanatic about a tool, but considering the trade-off and choosing the most suitable one for you according to your needs.

What about Carbon?

If you never heard about Carbon, let me explain: Carbon is a successor programming language for C++ which is started by Google. It’s not released yet and is still experimental, but it’s planned to be released in 2024 if there is no unexpected problem. Because of that, I cannot compare this language with others since the language is not completed yet.

In Quora, I saw lots of questions about Carbon replacing C++. I don’t understand such questions, because, on the GitHub page of the project, the creators of it wrote clearly that Carbon is not about replacing C++. It’s like one more abstraction layer for C++. The same is done for other languages, for instance, TypeScript is designed for JavaScript, and Kotlin is designed for Java. However, they haven’t replaced their ancestor languages. Let me prove it: Again we will take a look at Google trends.

JavaScript and TypeScript Google trends comparison
Java and Kotlin Google trends comparison

As you can see, people generally use the main language still. But it doesn’t mean that the successor languages are not useful. I listened to lots of developers who are very happy about the successor languages’ features.

I hope, we will like Carbon as well, and to be honest, according to what they wrote on the project GitHub page, it sounds very good and interesting. There is a good potential to write more and better code whose performance might be close to C++. However, nothing will change. We will just have another tool to use in our projects and we will still decide according to our needs. As a result, I recommend you to be open to new projects, and new tools, but be careful about the trade-offs between them and choose which one to use wisely.

Thanks for your time, and kind regards.

--

--