Rust vs Go in 2024

Rust vs Go in 2024

rust-vs-go.png

Can I say something without everyone getting mad?

Which is better, Rust or Go? Which language should you choose for your next project, and why? How do the two compare in areas like performance, simplicity, safety, features, scale, and concurrency? What do they have in common, and where do they fundamentally differ? Let’s find out, in this friendly and even-handed comparison of Rust and Golang.

Rust and Go are both awesome

First, it’s really important to say that both Go and Rust are absolutely excellent programming languages. They’re modern, powerful, widely-adopted, and offer excellent performance.

Rust is a low-level statically-typed multi-paradigm programming language that’s focused on safety and performance.

Gints Dreimanis

Whereas:

Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software.

golang.org

In this article, I’ll try to give a brief overview of where I think Go is the ideal choice, and where I think Rust is a better alternative.

Getting started with Go

If you’d like a friendly, no-nonsense introduction to programming in Go for beginners, there’s a book I can recommend (since I wrote it myself).

 
$44.95
Add To Cart
 

Get 30% off your purchase by entering the code RUSTVSGO at the checkout!

For the Love of Go assumes no previous knowledge of coding, and walks you through the essential concepts of Go: functions, variables, packages. As you progress through the book, you’ll be developing more sophisticated Go programs, using features like structs, maps, slices, and custom types, and with a strong emphasis on testing and test-driven development.

If you’re a more experienced programmer, the book will help you to get to grips with Go right away, explaining more advanced concepts like pointers, methods, defer, first-class functions, iota, and variadic functions. There’s something useful in it for everyone—I hope!

You can also find lots of Go tutorials, and more of my Go books right here on this site. If you’d like to get regular updates about new books and blog posts, plus exclusive free content and discount codes, join my Go Club mailing list (enter your email address at the bottom of this page).

Getting started with Rust

You can’t do better here than Steve Klabnik and Carol Nichols’s The Rust Programming Language, 2nd Edition.

It’s the original, official, and best Rust book out there, covering all the topics that tend to baffle new Rustaceans, including ownership, lifetimes, traits, dependency management, unsafe, mutability, cloning, and macros.

At over 500 pages, it’s a massive tome, and not for the faint-hearted, but this may be the only Rust book you’ll ever need.

Similarities

What are some of the common goals of both languages?

Memory safety

Historically, one of the biggest causes of software bugs and security vulnerabilities has been accessing memory unsafely or incorrectly.

Rust and Go deal with this problem in different ways, but both aim to be smarter and safer than other languages about managing memory.

Fast, compact executables

They’re both compiled languages, which means your programs are translated directly to executable machine code, so that you can deploy your program as a single binary file. This also makes both Rust and Go programs extremely fast in comparison to interpreted languages such as Python or Ruby.

General-purpose languages

Rust and Go are also both powerful, scalable general-purpose programming languages, which you can use to develop all kinds of modern software. Both have excellent standard libraries and a thriving third-party ecosystem, as well as great commercial support and a large user base.

Pragmatic programming style

While both Go and Rust have features associated with functional and object-oriented programming (OOP), they’re pragmatic languages aimed at solving problems in whatever way is most appropriate.

Development at scale

Both Rust and Go have some useful features which make them suitable for programming in the large, whether that means large teams, or large codebases, or both.

For example, both Rust and Go use a standard code formatting tool (gofmt for Go, rustfmt for Rust), putting an end to useless arguments over where to put your brackets.

Both also have excellent, built-in, high-performance standard build and dependency management tools; no more wrestling with complex third-party build systems and having to learn a new one every couple of years.

Differences

While Rust and Go have a lot in common, there are also a few areas where a reasonable person might prefer one language over the other, to meet the specific needs of their project.

Performance

Both Go and Rust are very fast. However, while Go’s design favours fast compilation, Rust is optimised for fast execution.

Rust’s run-time performance is also more consistent, because it doesn’t use garbage collection. On the other hand, Go’s garbage collector takes some of the burden off the programmer, making it easier to focus on solving the main problem, rather than the fine detail of memory management.

Rust is a better choice for areas where speed of execution beats all other considerations, such as game programming, operating system kernels, web browser components, and real-time control systems.

Simplicity

Go is a small language, by design: it has very little syntax, few keywords, and as few language constructs as it can get away with. You can learn the basics of Go and be productive in the language very quickly.

That gives Go the advantage in projects with a short timescale, or for teams that need to onboard lots of new programmers quickly, especially if they’re relatively inexperienced.

Features

At the other end of the scale, Rust has just about every feature you could imagine in a programming language, and some you probably can’t. That makes it a powerful and expressive language, with lots of different ways to do the same thing.

If you’re transitioning to Rust from some other language, you can probably find Rust equivalents for most of the features you’re used to. That gives Rust the advantage when large projects need to be migrated from a traditional language such as C++ or Java.

Concurrency

Unlike most languages, Go was designed with built-in features for concurrent programming, such as goroutines (a lightweight version of threads) and channels (safe and efficient ways to communicate data between concurrent tasks).

These make Go the perfect choice for high-scale concurrent applications such as webservers and microservices.

Safety

Rust is carefully designed to ensure that programmers can’t do something unsafe that they didn’t mean to do, such as overwriting a shared variable. The compiler requires you to be explicit about the way you share data between different parts of the program, and can detect many common mistakes and bugs.

As a result, so-called “fighting with the borrow checker” is a common complaint among new Rust programmers. Implementing your program in safe Rust code often means fundamentally re-thinking its design, which can be frustrating, but the benefits can be worth it when reliability is your top priority.

Scale

Go was designed to make it easy to scale both your projects and your development teams. Its minimalist design leads to a certain uniformity, and the existence of a well-defined standard style means that any Go programmer can read and understand a new codebase relatively quickly.

When it comes to software development in the large, clear is better than clever. Go is a good choice for big organisations, especially with many distributed teams. Its fast build times also favour rapid testing and deployment.

Trade-offs

Rust and Go’s design teams have made some starkly different choices, so let’s look at some areas where those trade-offs make the two languages very distinct from one another.

Garbage collection

Languages (like Go) that feature garbage collection, and automatic memory management in general, make it quick and easy to develop reliable, efficient programs, and for some people that’s the most important thing.

But garbage collection, with its performance overhead and stop-the-world pauses, can make programs behave unpredictably at run-time, and some people find this inconsistency unacceptable.

Languages (such as Rust) where the programmer must take explicit responsibility for allocating and freeing every byte of memory, are better for real-time or ultra-high-performance applications.

Abstraction

The history of computer programming has been a story of increasingly sophisticated abstractions that let the programmer solve problems without worrying too much about how the underlying machine actually works.

That makes programs easier to write and perhaps more portable. But for many programs, access to the hardware, and precise control of how the program is executed, are more important.

Rust aims to let programmers get “closer to the metal”, with more control, but Go abstracts away the architectural details to let programmers get closer to the problem.

Speed

Rust makes a number of design trade-offs to achieve the best possible execution speed. By contrast, Go is more concerned with simplicity, and it’s willing to sacrifice some (run-time) performance for it.

Whether you favour Rust or Go on this point depends on whether you spend more time waiting for your program to build, or waiting for it to run.

Correctness

Go and Rust both aim to help you write correct programs, but in different ways: Go provides a superb built-in unit testing framework, for example, and a rich standard library, while Rust is focused on eliminating run-time bugs using its borrow checker.

It’s probably fair to say that it’s easier to write a given program in Go, but the result may be more likely to contain bugs than the Rust version. Rust imposes discipline on the programmer, but Go lets the programmer choose how disciplined they want to be about a particular project.

What now?

I hope this article has convinced you that both Rust and Go deserve your serious consideration. You should reject the false dilemma that you can only learn one or the other. In fact, the more languages you know, the more valuable you are as a software developer.

Every new language you learn gives you new ways of thinking about problems, and that can only be a good thing. The most important factor in the quality and success of any software project is not the choice of language, but the skill of the programmer.

You will be most skilful when using the language that suits you best, and that you enjoy programming in the most. So, if the question is “Should I learn Rust or Go?”, the only right answer is “Yes.”

Cryptography in Go: AES explained

Cryptography in Go: AES explained

Best Go books for 2024

Best Go books for 2024

0