Comparing Rust and Go in 2022
ADVERTISEMENT
Table of Contents
- Introduction
- Overview
- Runtime Execution Speed of Rust and Go
- Development Speed of Rust and Go
- The Ecosystem
- Memory Management
- Real World Use Cases
- Summary
- Next Steps
Introduction
In this article, we'll discuss some key elements to consider when comparing Rust vs Go. Programmers that know one or both of these languages highly sought after and companies are willing to pay a premium price to developers that are proficient in either language.
Many think these two languages are battling each other for space in the industry, but that is not the case. Both languages will be around for a very long time to come, so you won't go wrong learning either language. However, it can be tricky to decide which language you would like to use more. To make that decision you need to be well informed about both languages. The goal of this article is to get you to a point where you can decide to start using either Rust or Go.
Overview
On the surface, Rust and Go might seem like they are similar languages looking to solve similar issues. However, as you start to look closer, you will begin to realize they are two entirely different languages looking to solve two entirely different problems.
Knowing that they are looking to solve different issues, we can start to break down both languages and look at some of the key differences. We will evaluate runtime and development speed, each language's ecosystem, how each language manages memory, and how each one is currently being used in production.
As I break down these components it is important to keep in mind what you think you might use either Rust or Go for since that will help you come to your final conclusion.
Runtime Execution Speed of Rust and Go
When talking about runtime speed, we are referring to fast the code executes - its performance. Rust and Go are both known for being high-performing languages, but one of them is the clear winner in terms of sheer execution speed.
Both Rust and Go are compiled to machine code, which is what allows them to have great runtime speed. However, Rust is almost always faster than Go, which we can see in these benchmarks.
These benchmarks are well known tests that evaluate the raw performance of a programming language's computational efficiency. To explain what a few of the programs do, we have the "n-body" benchmark for simulating orbits of Jovian planets, "fasta" for generating and writing random DNA sequences, and "reverse-complement" to read DNA sequences and write their reverse complement.
These benchmarks measure how much memory the program used, the CPU load, and the time taken for the program to complete. Rust can be up to an order of magnitude faster (10x as fast!) especially when considering advanced algorithms and resource intensive programs. The reason Rust is more performant is due to how Rust manages memory and how well the compiler is able to add a plethora of optimizations to the code.
Now that we know Rust is the winner in runtime performance, which one wins in terms of development speed or coding time by the developer?
Development Speed of Rust and Go
This is an important question because it is actually one of the issues Go was designed to solve. Go succeeded in creating a very simple language that emphasizes fast development time over execution speed, and Go is the winner over Rust in this category.
You might be wondering what was the reasoning behind this design principle? Luckily for us, the Go developer FAQ answers this question directly:
"Programming had become too difficult and the choice of languages was partly to blame. One had to choose either efficient compilation, efficient execution, or ease of programming; all three were not available in the same mainstream language. Programmers who could were choosing ease over safety and efficiency by moving to dynamically typed languages such as Python and JavaScript rather than C++ or, to a lesser extent, Java."
To summarize their answer, Google wanted something fast, safe, and easier than C/C++, but there was not a programming language out there that did this, so they created one. It is very well known that C and C++ are difficult programming languages to learn.
Google also wanted a language that could compile large projects in seconds. Go is unbelievably fast when it comes to compiling a project. Not to mention the language is, without a doubt, much easier to pick up on than Rust. Taking all this information into account between, Go is the clear winner over Rust in terms of dev speed.
The Ecosystem
Both Rust and Go are extremely popular coding languages today. There is a high job demand for someone who has the skills to use either one. It is important to remember though that both languages are still considered young in the grand scheme of things.
Since they haven't been around for as long as other languages like C++, Java, Python, and JavaScript, you must remember that Rust and Go will not have quite as many libraries available to them. However, both languages have very healthy ecosystems.
At the time this article was published Rust has been starred over 63.8k times on Github:
Go has been starred over 95.8k times:
These are very healthy indicators for the popularity of each language's ecosystem. Furthermore, Rust has been voted the number one language by developers on the Stack Overflow developer survey six years in a row. Go ranked 10th on the same survey.
We can also look at the TIOBE index, which we can use to gauge the popularity of programming languages. On the TIOBE index, Rust ranks 24th in popularity and Go comes in at 11th. Go's ranking is pretty amazing considering every language that is ranked higher, has been around substantially longer. All in all, both ecosystems are extremely healthy and very close on par with each other.
Memory Management
Let's start by discussing memory management with respect to Go. Go uses garbage collection, which you might be familiar with if you have a Python, Java, C#, or Javascript background.
If you are not familiar with garbage collection, it is a process that many programs use to free up memory that is no longer being used by the executing program. The upside to this is that the programmer does not have to manually manage memory. Manual memory management while programming can be complex, which is one of the reasons languages like C and C++ can be tough to master.
Go's utilization of garbage collection impacts runtime performance to some degree, which is part of the reason Go is not as performant as Rust during program execution. The extra overhead associated with having a garbage collector does affect performance.
On the other hand, Rust takes a unique approach to managing memory. Rust utilizes a system known as ownership and borrowing. This system is inherently gives memory safety to the program.
Ownership involves establishing a set of rules that govern how memory is managed in Rust. As a developer, this is typically the hardest concept to get comfortable with as it requires some new coding concepts that are not as common in more traditional languages.
For example, we are all comfortable with the following piece of code as all we are doing is assigning an integer value to variable x
, and then reassigning that value to variable y
. Then we print both variables:
let x = 1;
let y = x;
println!("x = {}, y = {}", x, y);
Next let's try to do something we are familiar with, but that will not work in Rust. We'll try to do the same operation we performed above but instead of an integer, we'll try using a string:
let string = String::from("String");
let copy_str = string;
println!("{}", string);
We are not going to do a deep dive into why one works and why the other does not work, but for now just know that the Rust compiler has strict rules embedded into it based on variable ownership. At a high level, there can only be one owner of a value at a time unless the type implements the "copy" trait. This allows the compiler to manually manage allocating and deallocating memory and ensure there are no dangling references.
Another cool aspect of Rust is a very friendly compiler to help us with these type of issues:
The compiler tells us exactly what the issue is and in some cases will even tell you exactly what to do in order to fix the issue. Compiler driven development is a popular method when developing with Rust. You type some code and then try to build it to see what the compiler outputs and then go from there if there are errors.
To build a program in Rust you simply run cargo run
which will build and run your program for you. If you just want to build the program, you can simply use cargo build
.
In Go, you just run go build .
in the directory of the code you want to execute.
Rust's memory management definitely provides a steep learning curve, (which is part of the reason we favored Go in the development speed comparison earlier on), but Rust's memory management system gives us C/C++ type performance and memory safety, which is exactly the issue Rust was trying to solve.
As noted in this blog posted by the Microsoft Security Response Center, roughly 70% of vulnerabilities Microsoft assigns to a CVE each year are memory safety issues. Rust's main design principle is to give the programmer high performant speed while also ensuring memory safety, which is a rare combination that we will continue to see used more and more in production systems.
Real World Use Cases
Now that we have a basic understanding of the key differences between Rust and Go, we can look at what companies use these languages and what they might be using these languages for. Go is currently being used by Google, PayPal, American Express, Twitter, Netflix and the list goes on and on.
Rust is being used by companies like Dropbox, Facebook, Google, and many more. There is also a discussion of allowing Rust into the Linux kernel, which would be a major breakthrough for the language. Besides that, Rust is being used for CLI (command-line interface) applications, webassembly, networking, systems programming, and embedded devices.
Go is very popular for cloud based and server-side applications. Two very popular tools that are written in Go are Docker and Kubernetes. It is very clear to see that both of these languages are used in mainstream production by the tech giants. Rust and Go will continue to be slowly migrated into more and more applications, growing the list of real world use cases.
Summary
In this article, we discussed some of the key differences between Rust and Go in 2022. We talked about each language's ecosystem and we were able to establish that both languages have a very healthy ecosystem which will continue to grow as the languages mature.
We evaluated their runtime and development speed and concluded that Rust is faster while executing. That is all thanks to its unique memory management system, known as ownership and borrowing. However, this adds to the learning curve for developers to get used to coding in Rust.
Go excels when it comes to development speed.
Overall, both languages are used by a wide variety of well established companies in order to improve their current technology stacks. It is safe to conclude that neither language is going away any time soon, but knowing which language to use, or learn, will definitely go a long way in providing a high quality product.
Next Steps
If you're interested in learning more about the basics of coding, programming, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.
Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.
Final Notes
Recommended product: Coding Essentials Guidebook for Developers