Rust vs Go

November 27, 2020

译者按:其实我只学过Golang,Rust啥的在本文发出时根本不懂,错了别打我哈哈哈哈哈。 原文地址:  

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, from the author of the For the Love of Go book series.

Rust和Go哪个更好?为了你的下一个项目应该选择哪个语言,为哈?在性能,简便,安全,特性,规模和并发上这两种语言该如何比较?它们有什么地方相同又有什么本质上的不同?让我们从For the Love of Go系列书籍作者的这篇友好又不失公正的比较中找到答案吧。

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. You may have read articles and blog posts aiming to convince you that Go is better than Rust, or vice versa. But that really makes no sense; every programming language represents a set of trade-offs. Each language is optimised for different things, so your choice of language should be determined by what suits you and the problems you want to solve with it.

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. Ideally, though, you should have a working familiarity with both languages. While they’re very different in syntax and style, both Rust and Go are first-class tools for building software. With that said, let’s take a closer look at the two languages.

Rust和Go都很棒 #



The similarities #

Rust and Go have a lot in common, which is one reason you often hear them mentioned together. What are some of the common goals of both languages?

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

Gints Dreimanis

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


相似性 #



Gints Dreimanis



Memory safety #

Both Go and Rust belong to the group of modern programming languages whose priority is memory safety. It’s become clear over many decades of using older languages such as C and C++ that one of the biggest causes of bugs and security vulnerabilities is 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, and to help you write correct and performant programs.

内存安全 #


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; unlike interpreted languages such as Python and Ruby, you don’t need to distribute an interpreter and lots of libraries and dependencies along with your program, which is a big plus. This also makes both Rust and Go programs extremely fast in comparison to interpreted languages.

快速紧凑的可执行程序 #


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, from web applications to distributed microservices, or from embedded microcontrollers to mobile apps. Both have excellent standard libraries and a thriving third-party ecosystem, as well as great commercial support and a large user base. They’ve both been around for many years, and will continue to be widely used for years to come. Learning either Go or Rust today will be a sound investment of your time and effort.

通用语言 #


Pragmatic programming style #

Neither are primarily functional languages (like Scala or Elixir, for example), and neither are exclusively object-oriented (like Java and C#). Instead, while both Go and Rust have features associated with functional and object-oriented programming, they’re pragmatic languages aimed at solving problems in whatever way is most appropriate, rather than forcing you into a particular way of doing things. (If you like the functional style of programming, though, you’ll find a lot more facilities for it in Rust, because Rust has a lot more facilities than Go in general.)

We can debate about what an ‘object-oriented’ language is, but it’s fair to say that the style of object-oriented programming that C++, Java, or C# users would expect is not present in either Go or Rust.

—Jack Mott

实用主义编程风格 #



—Jack Mott

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, whereas C programmers have argued for years about where to put their brackets, and whether code should be indented with tabs or spaces, both Rust and Go eliminate such issues completely by using a standard formatting tool (gofmt for Go, rustfmt for Rust) which rewrites your code automatically using the canonical style. It’s not that this particular style is so wonderful in itself: it’s the standardisation which Rust and Go programmers appreciate.

gofmt’s style is no one’s favourite, yet gofmt is everyone’s favourite.

Rob Pike

Another area where both languages score highly is in the build pipeline. Both 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.

Building Go and Rust code, having come from a Java and Ruby background in my early career, felt like an impossible weight off my shoulders. When I was at Google, it was a relief to come across a service that was written in Go, because I knew it would be easy to build and run. This has also been true of Rust, though I’ve only worked on that at much smaller scale. I’m hoping that the days of infinitely configurable build systems are dead, and languages all ship with their own purpose-built build tools that just work out of the box.

Sam Rose

大规模开发 #




Rob Pike



Sam Rose

So what’s all the fuss about? #

With all that in mind, and seeing that both languages are so well-designed and powerful, you might be wondering what all the holy wars are about (me too). Why do people make such a fuss about ‘Go versus Rust’, getting into angry social media spats and writing long blog posts about how only an idiot would use Rust, or that Go isn’t a real programming language, or whatever. It might make them feel better, but it doesn’t exactly help you, as someone trying to decide which language to use for your project, or which you should learn to advance your programming career. A wise person doesn’t make important choices based on who shouts the loudest.

Let’s continue our grown-up discussion now by looking at some areas where a reasonable person might prefer one language over the other.

所以有什么好大惊小怪的? #



Performance #

We’ve said that both Go and Rust produce extremely fast programs because they’re compiled to native machine code, without having to go through an interpreter or virtual machine. However, Rust’s performance is particularly outstanding. It’s comparable with C and C++, which are often regarded as the highest-performance compiled languages, but unlike these older languages, it also offers memory safety and concurrency safety at essentially no cost in execution speed. Rust also allows you to create complex abstractions without paying a performance penalty at run-time.

By comparison, although Go programs also perform extremely well, Go is primarily designed for speed of development (including compilation), rather than speed of execution. The Go compiler doesn’t spend a lot of time trying to generate the most efficient possible machine code; it cares more about compiling lots of code quickly. So Rust will usually beat Go in run-time benchmarks.

Rust’s run-time performance is also consistent and predictable, because it doesn’t use garbage collection. Go’s garbage collector is very efficient, and it’s optimised to make its stop-the-world pauses as short as possible (and getting shorter with every new Go release). But garbage collection inevitably introduces some unpredictability in the way programs behave, which can be a serious issue in some applications, such as embedded systems.

Because Rust aims to give the programmer complete control of the underlying hardware, it’s possible to optimise Rust programs to be pretty close to the maximum theoretical performance of the machine. This makes Rust an excellent 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 #

It doesn’t matter how fast a programming language is if nobody can figure out how to use it. Go was deliberately conceived as a reaction against the ever-growing complexity of languages like C++; it has very little syntax, very few keywords, and, indeed, few features. This means it doesn’t take long to learn the Go language to the point where you can write useful programs in it.

Go is incredibly easy to learn. I know this is an often-touted benefit, but I was really surprised at how quickly I was able to be productive. Thanks to the language, docs, and tools, I was writing interesting, committable code after literally two days.

Early Impressions of Go From a Rust Programmer

The key word here is simplicity. Simple isn’t the same as easy, for sure, but a small, simple language is easier to learn than a large, complex one. There just aren’t that many different ways to do things, so all well-written Go code tends to look the same. It’s easy to just dive into an unfamiliar service and understand what it’s doing.

fmt.Println("Gopher's Diner Breakfast Menu")
for dish, price := range menu {
    fmt.Println(dish, price)

(In the Code Club series, I do exactly that: pick Go projects semi-randomly from GitHub and explore them with a group of Go beginners, to see how much of the code we can understand. It always turns out to be more than we expected!)

Although the core language is small, Go’s standard library is very powerful. That means your learning curve will also need to include the parts of the standard library that you need, not just Go syntax. On the other hand, moving functionality out of the language and into the standard library means that you can focus on learning only the libraries that are relevant to you right now.

Go is also designed for software development at scale, with large codebases and large teams. In these situations, it’s important that new developers can get up to speed as quickly as possible.

With Go, you get things done—fast. Go is one of the most productive languages I’ve ever worked with. The mantra is: solve real problems today.

Matthias Endler

简便性 #



Early Impressions of Go From a Rust Programmer


fmt.Println("Gopher's Diner Breakfast Menu")
for dish, price := range menu {
    fmt.Println(dish, price)

Code Club系列中,我确实做到了:半随机地从GitHub中挑选Go项目并且和一群Go初学者探索它们,来看看我们能看懂多少代码。经常比我们预期要多!




Matthias Endler

Features #

Rust supports more complexity than several other programming languages, therefore, you can achieve more with it. For example, it supports generics.


Rust is specifically designed to include lots of powerful and useful features to help programmers do the most with the least code. For example, Rust’s match feature lets you write flexible, expressive logic quite concisely:

fn is_prime(n: u64) -> bool {
   match n {
       0...1 => false,
       _ => !(2..n).any(|d| n % d == 0),

Because Rust does a lot, this means there’s a lot to learn, especially at the start. But that’s okay: there’s a lot to learn in C++ or Java, too, and you don’t get the advanced features that come with Rust, like memory safety. To criticise Rust for being a complex language misses the point: it’s designed to be expressive, which means having a lot of features, and in many situations that’s what you want from a programming language. There’s a learning curve, for sure, but once you’re up and running with it, you’ll be fine.

Rust competes for mindshare with C++ and D for programmers who are prepared to accept more complex syntax and semantics (and presumably higher readability costs) in return for the maximum possible performance.

Dave Cheney

特性 #




fn is_prime(n: u64) -> bool {
    match n {
        0...1 => false,
        _ => !(2..n).any(|d| n % d == 0),



Dave Cheney

Concurrency #

Most languages have some form of support for concurrent programming (doing multiple things at once), but Go was designed for this job from the ground up. Instead of using operating system threads, Go provides a lightweight alternative: goroutines. Each goroutine is an independently executing Go function, which the Go scheduler will map to one of the OS threads under its control. This means that the scheduler can very efficiently manage a large number of concurrent goroutines, using only a limited number of OS threads.

Consequently, you can run millions of concurrent goroutines in a single program without creating serious performance problems. That makes Go the perfect choice for high-scale concurrent applications such as webservers and microservices.

Go also features fast, safe, efficient ways for goroutines to communicate and share data, using channels. Go’s concurrency support feels well-designed, and a pleasure to use. Reasoning about concurrent programs is hard in general, and building reliable, correct concurrent programs is a challenge in any language. Because it was built into the language from the start, though, instead of being an afterthought, concurrent programming in Go is about as simple and well-integrated as it could reasonably be.

Go makes it very easy to build a nicely factored application that takes full advantage of concurrency while being deployed as a set of microservices. Rust can do those things, too, but it’s arguably a bit tougher. In some respects, Rust’s obsession with preventing memory-related security vulnerabilities means that programmers have to go out of their way to perform tasks that would be simpler in other languages, including Go.

Sonya Koptyev

The concurrency story in Rust is very new, by comparison, and still stabilising, but it’s under very active development, so watch this space. For example, Rust’s rayon library provides a very elegant and lightweight way of turning sequential computations into parallel ones.

Having lightweight syntax for spawning Go routines and using channels are really nice. It really shows the power of syntax that such small details make concurrent programming feel so much nicer than in other languages.

Early Impressions of Go From a Rust Programmer

While it may be a bit less straightforward to implement concurrent programs in Rust, it’s still possible, and those programs can take advantage of Rust’s guarantees about safety. A good example is the standard library’s Mutex class: in Go, you can forget to obtain a mutex lock before accessing something, but Rust won’t let you do that.

Go is focused on concurrency as a first class concept. That is not to say you cannot find aspects of Go’s actor oriented concurrency in Rust, but it is left as an exercise to the programmer.

Dave Cheney

并发 #





Sonya Koptyev



Early Impressions of Go From a Rust Programmer



Dave Cheney

Safety #

We saw earlier that both Go and Rust aim, in different ways, to prevent a large class of common programming errors, to do with memory management. But Rust in particular goes to great lengths to ensure that you can’t do something unsafe that you didn’t mean to do.

Rust’s very strict and pedantic compiler checks each and every variable you use and every memory address you reference. It avoids possible data race conditions and informs you about undefined behavior. Concurrency and memory safety issues are fundamentally impossible to get in the safe subset of Rust.

Why Rust?

This is going to make programming in Rust a different experience to almost all other languages, and it may be a challenging one at first. But for many people, the hard work is worth it.

For me the key advantage of Rust is a feeling that the compiler has my back and won’t let through any bug it could possibly detect (seriously, it feels like magic sometimes).

—Grzegorz Nosek

Many languages, including Go, have facilities to help programmers avoid mistakes, but Rust takes this to a new level, so that potentially incorrect programs won’t even compile.

With Rust, the library programmer has a lot of tools to prevent her users making mistakes. Rust gives us the ability to say that we own a specific piece of data; it’s not possible for anything else to claim ownership, so we know nothing else will be able to modify it. I can’t think of a time I’ve ever been given this many tools to prevent accidental misuse before. It’s a wonderful feeling.

Sam Rose

“Fighting with the borrow checker” is a common syndrome for new Rust programmers, but in most cases the problems that it finds are genuine bugs (or at least potential bugs) in your code. It may force you to fundamentally re-architect your program to avoid running into these issues; and that’s a good thing, when correctness and reliability are your top priority. What’s the point of a language that doesn’t change the way you program? The lessons that Rust teaches about safety can be useful when you’re working in other languages, too.

If you choose Rust, usually you need the guarantees that the language provides: safety against null pointers and data races, predictable runtime behaviour, and total control over the hardware. If you don’t require any of these features, Rust might be a poor choice for your next project. That’s because these guarantees come with a cost: ramp-up time. You’ll need to unlearn bad habits and learn new concepts. Chances are, you will fight with the borrow checker a lot when you start out.

Matthias Endler

How challenging you find Rust’s programming model probably depends on what previous experience you have in other languages. Python or Ruby programmers may find it restrictive; others will be delighted.

If you’re a C or C++ programmer who’s spent weeks chasing down memory safety bugs in those languages, you’ll really appreciate Rust. “Fighting the borrow checker” becomes “The compiler can detect that? Cool!”

—Grzegorz Nosek

安全 #



Why Rust?



—Grzegorz Nosek



Sam Rose

“和borrow checker搏斗”是新晋Rust程序员的共同经历,但大多情况下它查到的问题确实是代码中的真正存在的bug(至少是潜在的bug)。它或许会迫使你把程序从根本上重构一遍来防止运行的时候出现这些问题;这是好的,当你把正确性和可靠性列为最高优先级。用一门没有改变你编程方式的语言有什么意义呢?Rust在安全上教的一课会在你使用其他语言时也能发挥用处。

如果你选择了Rust,你经常需要保证满足语言的这些要求:对空指针和数据竞赛的安全性,可预测的运行时行为,以及对于硬件的完全控制。如果你不具备任一这些特点,Rust或许在你下一个项目将成为糟糕的选择。因为这些要求都需要同一个代价:爬坡时间。你需要忘却陋习和学习新知。而且很可能你在一开始在borrow checker中挣扎很长一段时间。

Matthias Endler


如果你是一个C/C++程序员,花费数周时间在语言中追查内存安全bug,你会非常欣赏Rust。“和borrow checker搏斗”会变成“这编译器连这个都能查?真不错!”

—Grzegorz Nosek

Scale #

Today’s server programs comprise tens of millions of lines of code, are worked on by hundreds or even thousands of programmers, and are updated literally every day. Go was designed and developed to make working in this environment more productive. Go’s design considerations include rigorous dependency management, the adaptability of software architecture as systems grow, and robustness across the boundaries between components.

Rob Pike

When you’re working on a problem by yourself or in small teams, the choice of a simple language or a rich language is a matter of preference. But as the software grows bigger and more complex, and the teams grow larger, the differences really start to show. For large applications and distributed systems, speed of execution is less important than speed of development: a deliberately minimal language like Go reduces the ramp-up time for new developers, and makes it easier for them to work with a large codebase.

With Go, it’s easier as a junior developer to be more productive, and harder as a mid-level developer to introduce brittle abstractions that will cause problems down the line. For these reasons, Rust is less compelling than Go for enterprise software development.

Loris Cro

When it comes to software development in the large, clear is better than clever. Go’s limitations actually make it more suitable for enterprises and big organisations than more complex and powerful languages such as Rust.

规模 #


Rob Pike



Loris Cro


The differences #

Although Rust and Go are both popular, modern, widely-used languages, they’re not really competitors, in the sense that they’re deliberately targeting quite different use cases. Go’s whole approach to programming is radically different to Rust’s, and each language will suit some people while irritating others. That’s absolutely fine, and if both Rust and Go did more or less the same things in more or less the same way, we wouldn’t really need two different languages.

So can we get a sense of the respective natures of Rust and Go by finding issues on which they take drastically different approaches? Let’s find out.

区别 #



Garbage collection #

“To garbage-collect, or not to garbage-collect” is one of those questions that has no right answer. Garbage collection, and automatic memory management in general, makes it quick and easy to develop reliable, efficient programs, and for some people that makes it essential. But others say that garbage collection, with its performance overhead and stop-the-world pauses, makes programs behave unpredictably at run-time, and introduces unacceptable latency. The debate rumbles on.

Go is a very different language to Rust. Although both can vaguely be described as systems languages or replacements for C, they have different goals and applications, styles of language design, and priorities. Garbage collection is a really huge differentiator. Having GC in Go makes the language much simpler and smaller, and easy to reason about.

Not having GC in Rust makes it really fast (especially if you need guaranteed latency, not just high throughput) and enables features and programming patterns that are not possible in Go (or at least not without sacrificing performance).


垃圾回收机制 #





Close to the metal #

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.

Both languages have a different scope. Golang shines for writing microservices and for typical “DevOps” tasks, but it is not a systems programming language. Rust is stronger for tasks where concurrency, safety and/or performance are important; but it has a steeper learning curve than Go.

Matthias Endler

贴近硬件 #



Matthias Endler

Must go faster #

I’ve written elsewhere that performance is less important than readability for most programs. But when performance does matter, it really matters. Rust makes a number of design trade-offs to achieve the best possible execution speed. By contrast, Go is more concerned about simplicity, and it’s willing to sacrifice some (run-time) performance for it. But Go’s build speed is unbeatable, and that’s important for large codebases.

Rust is faster than Go. In the benchmarks above, Rust was faster, and in some cases, an order of magnitude faster. But before you run off choosing to write everything in Rust, consider that Go wasn’t that far behind it in many of those benchmarks, and it’s still much faster than the likes of Java, C#, JavaScript, Python and so on.

If what you need is top-of-the-line performance, then you’ll be ahead of the game choosing either of these two languages. If you’re building a web service that handles high load, that you want to be able to scale both vertically and horizontally, either language will suit you perfectly.

Andrew Lader

必须够快 #




Andrew Lader

Correctness #

On the other hand, a program can be arbitrarily fast if it doesn’t have to work properly. Most code is not written for the long term, but it’s often surprising how long some programs can stay running in production: in some cases, many decades. In these situations it’s worth taking a little extra time in development to make sure that the program is correct, reliable, and won’t need a lot of maintenance in the future.

My take: Go for the code that has to ship tomorrow, Rust for the code that has to keep running untouched for the next five years.

—Grzegorz Nosek

While both Go and Rust are great choices for any serious project, it’s a good idea to make yourself as well-informed as possible about each language and its characteristics. Ultimately, it doesn’t matter what anyone else thinks: only you can decide which is right for you and your team.

If you want to develop faster, perhaps because you have many different services to write, or you have a large team of developers, then Go is your language of choice. Go gives you concurrency as a first-class citizen, and does not tolerate unsafe memory access (neither does Rust), but without forcing you to manage every last detail. Go is fast and powerful, but it avoids bogging the developer down, focusing instead on simplicity and uniformity. If on the other hand, wringing out every last ounce of performance is a necessity, then Rust should be your choice.

Andrew Lader

正确性 #



—Grzegorz Nosek



Andrew Lader

Conclusion #

I hope this article has convinced you that both Rust and Go deserve your serious consideration. If at all possible, you should aim to get at least some level of experience in both languages, because they will be incredibly useful to you in any tech career, or even if you enjoy programming as a hobby. If you only have time to invest in learning one language well, don’t make your final decision until you’ve used both Go and Rust for a variety of different kinds of programs, large and small.

And knowledge of a programming language is really only a small part of being a successful software engineer. By far the most important skills you’ll need are design, engineering, architecture, communication, and collaboration. If you excel at these, you’ll be a great software engineer regardless of your choice of language. Happy learning!

结论 #