Rust vs Go, how to choose?

[CSDN editor’s note] John Arundel, a well-known Go developer and writer with more than 40 years of programming experience, shared the article “Rust vs Go in 2023” on his personal blog. This article pointed out the advantages and disadvantages of the two languages ​​very pertinently, and Applicable scenarios are definitely a very good reference article for developers who don’t know how to choose a programming language.

Original address: https://ift.tt/eTADah7

Unauthorized reprinting is prohibited!

Author| Compiled by John Arundel | Wang Ziyu

Produced | CSDN (ID: CSDNnews)

Today, if you were asked to choose the most suitable programming language for your application, which would it be? Why?

So, how do Rust and Go compare in terms of performance, simplicity, security, features, scale, etc.? What do they have in common, and how are they fundamentally different? This article reveals the answer for you.

Arguing about good programming languages ​​is pointless

First of all, both Go and Rust are very good programming languages. They are widely used due to their excellent performance. You may often see some articles discussing the advantages and disadvantages of Go and Rust, which is actually meaningless, and each programming language represents certain trade-offs and trade-offs. Different programming languages ​​will optimize different content in a targeted manner. Only by choosing the most suitable language can we get twice the result with half the effort.

This article will tell you when it is appropriate to choose Go and when it is better to use Rust, and will tell you the essence of the two programming languages, the Tao of Go and Rust. While they differ greatly in syntax and style, both Rust and Go are first-rate tools for building software.

Go, Rust: Similarities

Rust and Go have a lot in common and are often discussed together. So, what are the common goals of these two languages?

Rust is a low-level statically typed, multi-paradigm programming language with a focus on safety and performance.

—Gints Dreimanis

However

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

——Golang.org

Both Go and Rust are modern programming languages ​​whose top priority is memory safety. Unsafe or improper access to memory is one of the biggest causes of bugs and security breaches in developers’ long-term use of traditional languages ​​such as C and C++.

Although Rust and Go have different ways of dealing with memory safety, they have the same goal. They are committed to surpassing other languages ​​​​in memory management and helping developers write normal and high-performance applications.

Since both are compiled languages, the program can be directly translated into executable machine code and deployed as a single binary file; unlike interpreted languages ​​such as Python and Ruby, we do not need an interpreter, a large number of The library and dependencies are distributed with the program, which is one of their more prominent advantages. Therefore, programs written in Rust and Go are faster than interpreted languages.

Both Rust and Go are extensible general-purpose programming languages. They can develop all kinds of modern software, whether it is from web applications to distributed microservices, or from embedded microcontrollers to mobile applications.

They all have excellent standard libraries and efficient third-party ecosystems, huge commercial support, and a huge user base. Rust and Go have a long history and will continue to be widely used for years to come. Therefore, learning Go or Rust is a reasonable investment of time and energy.

Neither language is primarily functional (Scala or Elixir) nor fully object-oriented (Java and C#). Conversely, while both Go and Rust have features related to functional and object-oriented programming, they are both pragmatic programming languages ​​that aim to solve problems in the most appropriate way, rather than forcing a particular approach. In general, if you like the functional programming style, you’ll find more facilities in Rust.

We can argue about what constitutes an “object-oriented” language, but in reality, the object-oriented programming style that users of C++, Java, or C# expect is not present in either Go or Rust.

——Jack Mott

Whether it’s large teams, large code bases, or both, some of Rust’s and Go’s strengths make them better suited for these large programs. For example, while C programmers have been arguing for years about where to put parentheses, and whether code should be indented with tabs or spaces, Rust and Go use standard formatting tools (gofmt for Go, rustfmt for Rust) and canonicalized style automatically rewrites the code, completely solving these problems. That’s not to say that this particular style is inherently good: both Rust and Go programmers appreciate this standardization.

No one likes gofmt’s style, but gofmt is everyone’s favorite.

—Rob Pike

Another area where both languages ​​score well is building pipelines. Both have excellent built-in high-performance standard build and dependency management tools; no more wrestling with complex third-party build systems or learning a new one every few years.

Early in my career, building Go and Rust code against a Java and Ruby background put me through. When I come across a service written in Go at Google, I’m relieved because I know it’s easy to get up and running. The same is true for Rust, although I’ve only worked on it on a smaller scale. I hope that the days of infinitely configurable build systems are over and that all languages ​​will have their own dedicated build tools out of the box.

—Sam Rose

Rust or Go?

Both are well-designed good programming languages. But there are still a lot of people making a fuss about “Go vs. Rust”, and even fighting on social media, writing long blog posts saying that only fools use Rust, or that Go is not a real programming language, etc.

The reason may be that this behavior makes them feel good about themselves, but it is meaningless. Those voices trying to decide for you which language to use for your projects, or telling you which language you should learn to advance your programming career, will not affect a wise person to make important choices.

Both Go and Rust generate very fast programs because they are compiled directly to native machine code without going through an interpreter or a virtual machine.

However, Rust’s performance is particularly good. It is comparable to C and C++, which are generally considered the highest performing compiled languages. But unlike these traditional languages, Rust not only provides memory safety and concurrency safety, but Rust is also capable of creating complex abstractions. More importantly, Rust uses zero-cost abstractions.

In contrast, Go’s program performance is also very good, and it is mainly designed for development speed (including compilation) rather than execution speed. Go programmers tend to value clean code over fast code.

The Go compiler also doesn’t spend a lot of time trying to generate the most efficient machine code; it’s more concerned with compiling large amounts of code quickly. So in runtime benchmarks, Rust usually wins.

Rust runtime performance is consistent and predictable because it does not use garbage collection. Whereas Go’s garbage collector is very efficient and optimized to make its STW time as short as possible (it gets shorter with each new Go version). But the garbage collector inevitably introduces some unpredictability into the way the program behaves, which can be a serious problem in some applications, such as embedded systems.

Because Rust is designed to give the programmer complete control over the underlying hardware, it is possible to optimize a Rust program to very close to the machine’s maximum theoretical performance. As a result, Rust outperforms all other areas of consideration, such as game programming, operating system kernels, web browser components, and real-time control systems, with remarkable execution speed.

Being fast is of no use if no one can figure out how to use a programming language. The Go language was purposely designed to deal with the growing complexity of languages ​​like C++, with very few syntax, keywords, and features.

It doesn’t take long to learn the Go language and write usable programs in it.

Go is very easy to learn. I know this is an oft-touted benefit, but I was really amazed at how quickly I was able to increase my productivity. Thanks to the language, documentation, and tools, I wrote interesting, usable code in just two days.

——A Rust Programmer’s Early Impressions of Go

The key word here is simplicity. Of course, simple doesn’t equate to easy, it’s just that small and simple languages ​​are easier to learn than large and complex ones. There aren’t many different ways of doing things, so all well-written Go code tends to look the same.

In my Code Club video series, I semi-randomly pick Go projects from GitHub and explore them with a group of Go beginners to see just how much code we can understand. The results are often more than we expected.

While the core language is small, Go’s standard library is very powerful. Therefore, we not only need to learn Go syntax, but also the required parts of the standard library. At the same time, moving functionality out of the language and into the standard library allows us to focus more.

Go is designed for large-scale software development, including large code bases and large teams. Therefore, in order to allow novice developers to get started as soon as possible, the Go community values ​​simple, clear, routine, and direct programs.

With Go, you can get things done quickly. Go is one of the most productive languages ​​I’ve ever used. Its slogan is: Solving Real Problems Today.

——Matthias Endler

Rust is more compatible than several other programming languages. Therefore, you can achieve more with it.

– Devathon

Rust is specifically designed to help programmers do the most with the least amount of code, and it’s functional. For example, Rust’s matching functionality lets you write flexible, expressive logic quite concisely.

We had a lot to learn about Rust in the beginning. But there’s a lot to learn in C++ or Java, and you won’t get Rust’s advanced features like memory safety.

Rust has been criticized for being a complex language because of its expressiveness and versatility. In many cases, this is exactly the programming language we need.

Rust offers the programmer more complex syntax and semantics (and possibly a higher cost of readability) in exchange for maximum possible performance, and thus strives for mind-share with C++ and D.

—Dave Cheney

While Rust borrows some of Go’s features, and so does Go (especially generics), it can be argued that Rust’s features are more complex, while Go’s features are relatively simple.

Most languages ​​provide some form of support for concurrent programming (doing multiple operations at once), but Go is designed for the job. Instead of using operating system threads, Go provides a lightweight alternative: goroutines.

Each goroutine is an independently executing Go function that the Go scheduler maps to one of the OS threads under its control. Therefore, the scheduler can manage a large number of concurrent goroutines very efficiently using only a limited number of OS threads.

Go is the best choice for high-scale concurrent applications that can run millions of concurrent goroutines in one program without serious performance issues, such as web servers and microservices.

Go is fast, safe, and efficient, allowing goroutines to better communicate and share data. At the same time, Go’s concurrency support design experience is also very good.

In general, reasoning about concurrent programs is hard. Building reliable, correct concurrent programs in any language is certainly a challenge. But Go is built into the language from the start, so its concurrent programming is the easiest and most complete.

Taking advantage of concurrency to build a great application and then deploy a set of microservices is a breeze with Go, not so easy with Rust. In some ways, Rust’s obsession with preventing memory-related security holes means that programmers have to go to great lengths to perform tasks that would be simpler in other languages, including Go.

—Sonya Koptyev

Concurrency in Rust, by contrast, is a fairly new feature and has yet to be stabilized, but it’s under active development, so keep an eye out for it. For example, Rust’s rayon provides a very elegant and lightweight way to convert sequential computations to parallel ones.

The lightweight Goroutine and Channel syntax is really cool and shows the power of the syntax that it’s such small details that make concurrent programming feel better than in other languages.

——A Rust Programmer’s Early Impressions of Go

While implementing concurrent programs in Rust is not trivial, Rust’s memory safety is certainly an advantage. Like standard library classes: in Go, you can forget to acquire a Mutex lock before accessing something, but not in Rust.

Go encourages concurrency and outperforms Rust in this regard, but that’s not to say you can’t find Go’s actor-oriented concurrency in Rust, it’s an exercise left to the programmer.

—Dave Cheney

Based on the foregoing, it is not difficult to see that both Go and Rust avoid a large class of common programming mistakes related to memory management in different ways. However, Rust’s security is stronger.

Rust’s compiler is very strict and pedantic, checking every variable used and every memory address referenced. It avoids some unknown risks. Therefore, concurrency and memory safety issues are impossible in Rust.

——Why Rust?

For me, the key strength of Rust is the compiler support that doesn’t let through any errors it might detect (seriously, this feels like magic).

——Grzegorz Nosek

In fact, many languages ​​​​have facilities to help programmers avoid errors, but Rust has more advantages in this regard, so potentially incorrect programs will not even be compiled.

With Rust, library programmers have many tools at their disposal to prevent users from making mistakes. Rust allows us to confidently say that we own a certain piece of data and other things don’t. There are so many tools to prevent accidental misuse. It’s a great feeling.

—Sam Rose

“Fighting with the borrow checker” is common for Rust newbies, but in most cases the problems it finds are indeed real bugs (at least potential bugs) in your code. To ensure correctness and reliability, it may force you to fundamentally re-architect your program to avoid these problems.

Doesn’t the language change the core of how programming is done? A course on safety in Rust is also very useful when you’re working in other languages.

If you choose Rust, it is usually based on the guarantees it can provide: safety against null pointers and data races, predictable runtime behavior, and complete control over the hardware. If you don’t need these features, Rust might not be the best choice for your next project. That’s because these guarantees come at a price: time to elevate. You need to break bad habits and learn new concepts. In the beginning, you will often fight a lot with the borrow checker (borrow checker).

——Matthias Endler

How challenging you find Rust’s programming model may depend on your previous experience with other languages. Python or Ruby programmers may find it limiting; others may be happy.

If you’re a C or C++ programmer who’s spent weeks hunting memory safety holes in those languages, you’ll really appreciate Rust. “Fighting the borrow checker” becomes “Can the compiler detect this?”

——Grzegorz Nosek

Today’s server programs consist of tens of millions of lines of code, processed by hundreds or even thousands of programmers, and are updated every day. The Go language was born to work in this environment. Design considerations for Go include strict dependency management as the system grows, adaptability of the software architecture as the system grows, and robustness across component boundaries

—Rob Pike

For large applications and distributed systems, speed of execution is not as important as speed of development: a deliberately minimal language like Go can reduce startup time for junior developers, making it easier for them to work with large code bases.

With Go, it is easier for a junior developer to be productive, and as an intermediate developer, it is more difficult to introduce some fragile abstractions, which will lead to problems in the next step. Based on this, Rust is not as convincing as Go for enterprise software development.

——Loris Cro

When it comes to large-scale software development, clarity trumps flexibility. Go’s restrictive nature actually makes it a better fit for enterprises and larger organizations than more complex and powerful languages ​​like Rust.

Rust, Golang: Differences

While both Rust and Go are commonly used languages, they are not really competitors because they target completely different use cases.

The whole approach to programming in Go is quite different from Rust’s, and has different applicability.

With that in mind, let’s compare the main differences between Rust and Go.

“Whether to garbage collect or not” is a question that has no right answer. Garbage collection, and automatic memory management in general, make developing reliable, efficient programs more efficient and, for some, essential.

But others say that garbage collection, with its performance overhead and pauses, makes programs behave unpredictably at runtime and introduces unacceptable latency.

Go is completely different from Rust. While both can be vaguely described as systems languages ​​or alternatives to C, they have different goals and applications, styles of language design, and priorities. Garbage collection is one of the most obvious differences. Using GC in Go makes the language simpler and easier to reason about.

Not having a GC in Rust makes programs faster (especially when you need guaranteed latency, not just high throughput), and enables features and programming patterns that are impossible in Go (or at least without without sacrificing performance).

——PingCAP

The history of computer programming has been a story of increasingly complex abstractions, where programmers solve problems without worrying too much about how the underlying machine actually works.

Therefore, programs are easier to write and more portable. But for many programs, access to hardware and precise control over how the program executes is more important.

Rust’s goal is to bring the programmer “closer to the metal”, with more control, but Go abstracts away the architectural details, bringing the programmer closer to the problem.

Both languages ​​have different scopes. Golang is great for writing microservices and typical “DevOps” tasks, but it’s not a systems programming language. Rust is better for tasks where concurrency, safety, or performance are important; but it has a steeper learning curve.

——Matthias Endler

Most people favor development speed over software speed or performance. But in reality, performance matters too. Rust makes some design trade-offs to improve execution speed.

In contrast, Go focuses more on simplicity, but Go’s build speed is unrivaled, especially for large code bases.

Rust is faster than Go. On benchmarks, Rust is faster, in some cases by an order of magnitude. But before you choose to write programs in Rust, you will find that Go does not lag behind it in many benchmarks, and is even much faster than Java, C#, JavaScript, Python, etc.

If what you need is first-class performance, then choose any of these two languages. If you’re building a web service that handles high loads and you want to be able to scale both vertically and horizontally, then either language will be a good fit for you.

—Andrew Lader

On the other hand, a program can be arbitrarily fast if it doesn’t need to run properly. Most code is not written for the long term, but some programs can run for decades.

In this case, it is worth spending a little more time in development to ensure the correctness and reliability of the program.

Both Go and Rust aim to help you write correct programs, albeit in different ways. For example, Go provides a nice built-in testing framework, while Rust focuses on eliminating errors at runtime with its borrow checker.

My take: choose Go for code that will be released tomorrow, and Rust for code that will stay the same for the next five years. —Grzegorz Nosek

While both Go and Rust are excellent choices for any serious project, it’s a good idea to familiarize yourself with each language and its idiosyncrasies as fully as possible.

If you want to speed up your development writing many different services, or you have a large development team, then Go is your language of choice. Go provides first-class concurrency, and also guarantees memory safety, without forcing you to manage every last detail.

Go is a productive language that avoids bogging down developers and focuses on simplicity and uniformity. If on the other hand, if you need screw performance, then Rust should be your choice.

—Andrew Lader

At the end of the day, the right language is the best.

Appropriate language is the best

Whether it is Rust or Go, it is actually worth our serious understanding, and we can even gain some experience to improve ourselves or our work. If you can only choose one, make your choice after using Go and Rust for various types of programs large and small.

A programming language is only one step in becoming a successful software engineer. Design, engineering, architecture, communication and collaboration are all essential. Only by doing these perfectly, no matter which language you choose, you will become an excellent software engineer.

The text and pictures in this article are from CSDN

loading.gif

This article is transferred from https://www.techug.com/post/rust-vs-go-how-do-you-choose2d01e410a5e0e155407f/
This site is only for collection, and the copyright belongs to the original author.