🚀 The special episode of "Dr. Han, What Do You Think" is live!
🎙 Gate.io Founder & CEO Dr. Han takes on a rapid fire Q&A, covering work, life, and some truly tricky questions!
👀 How will he tackle these challenges?
🤩 Click to watch his real-time reactions, and join in the comments!
Rust VS Go, which language should developers choose in 2023?
Source: InfoQ
In 2023, we have a thousand reasons to learn Rust.
On August 7, the Rust Foundation released the results of the 2022 Rust Survey Report. The report shows that the adoption rate of Rust continues to increase. More than 90% of the survey respondents indicated that they are Rust users; Rust is used for the majority of coding work in , a significant increase of 51.8% over the previous year.
There is no doubt that Rust is increasingly becoming the focus of developers with its excellent memory safety and concurrency performance. However, it is also difficult to ignore Go, a relatively "veteran" player who was once selected as the programming language of the year.
Born in 2009, the Go language has attracted great attention since its unique concurrency model and powerful performance advantages. It is worth noting that, like the Rust language, the creator of the Go language also "hates" C++, and Go is also the dominant language of cloud native.
In the Stack Overflow 2022 developer survey, among the 70,000 responses to the question of "a programming language that makes people love and hate", programmers obviously prefer Rust, 86% of them said they like Rust, and 64% said they like Go. Facing the hot status of Rust, some developers have asked their souls: Is Go still worth learning in 2023?
In addition, in the past two days, whether to choose Rust or Go has also become a hot topic on Hacker News:
A netizen who supports Rust said: "I have also been troubled by this choice for a long time. In the end, Rust won. First, I feel that Rust is closer to the things of the previous Pascal era, and you can control everything; second, if wasm and related technologies are big outbreak, Rust would be a safer choice; then, we already have Python for rapid development, so it makes sense to choose something a little more extreme, and Go is somewhat in the middle ground. Finally, Rust should be used for Core and high-profile, so it is unlikely to be eliminated."
Another developer who disagreed said, "I have been developing in Go for almost ten years, but recently I also tried Rust. I think there is currently some mandatory and misleading preference for Rust, from my experience in From experience at various startups, including my current one, Go is by far the best choice for backend development! Note, performance, functionality or otherwise...the two languages are very, very similar! "
I have to say that both Go and Rust are definitely excellent programming languages. They're modern, powerful, versatile, and deliver great performance. But it really doesn't make sense to directly compare Go and Rust, who is better, because each programming language represents a series of deep trade-offs behind it. Different languages will be optimized for different needs, so when we choose a language, we should also consider what kind of problems we want to use it to solve. So we will start from the applicable scenarios of Go and Rust languages, and discuss the "way" of Go and Rust's design.
While Rust and Go differ greatly in syntax and style, both are first-class tools for building software. Let's start the specific analysis below.
Go vs. Rust: Similarities
Rust and Go have a lot in common, which is why people often compare the two. What goals do they have in common?
and:
Memory Safety
Both Go and Rust are among the modern programming languages that place emphasis on memory safety. Over the decades of development of older languages like C and C++, it has become clear that one of the core causes of errors and bugs is unsafe/incorrect access to memory.
So Rust and Go each gave different solutions, but the goal of both is to be smarter and safer in terms of memory management, and to help developers write correct programs with excellent performance.
Fast, compact executable
Both are compiled languages, which means that programs can be translated directly into executable machine code, so that programs can be deployed as a single binary file. Unlike interpreted languages like Python and Ruby, we don't need to ship an interpreter and a lot of libraries/dependencies with the program. As a direct reflection of this core strength, Rust and Go programs tend to run faster than interpreted languages.
General Purpose Language
Both Rust and Go are powerful and extensible general-purpose programming languages that you can use to develop all kinds of modern software—from web applications to distributed microservices, embedded microcontrollers, mobile applications, and more. **Both have excellent standard libraries and thriving third-party ecosystems, plus strong commercial support and a large user base. **Both have been around for years and will continue to thrive in the years to come. Learning Go or Rust would be a very reasonable investment of time and energy these days.
Pragmatic programming style
Neither are overly functional languages (such as Scala or Elixir), nor are they fully object-oriented (such as Java and C#). On the contrary, although both Go and Rust have the functions of functional and object-oriented programming, they always emphasize the pragmatic orientation-that is, solve the problem in the most appropriate way, rather than forcing everyone to use a specific method through "ideology". way to do things. **But if you really like the functional programming style, there are more related tool options on the Rust side, which is one of the advantages of Rust over Go. **
Massive Development
Both Rust and Go provide many useful features for large-scale programming, so they can both adapt to the real-world needs of large development teams and large code bases. **For example, C programmers have debated for years about where to put parentheses, and whether code should be indented with tabs or spaces; but Rust and Go already use standard formatting tools (Go has gofmt, Rust It is rustfmt) completely solve these problems. They automatically rewrite your code using a conforming style.
It's not that this particular format is subtle, but **Rust and Go programmers are more pragmatic and would rather choose a uniform standard of implementation. **
Another big advantage of these two languages is reflected in the build pipeline. Both have excellent, built-in, and performant standard build and dependency management tools. This means that programmers don't have to contend with complex third-party build systems, nor do they need to learn a new system every few years.
**Rust or Go? **
After talking about so many issues, and the fact that both languages are so well-designed and powerful, is there any result in this competition? Or, since both are excellent options, why are people still outraged on social media, writing long commentary blog posts saying "Rust is for idiots" or "Go is not a programming language at all" Kind of harsh words?
Of course, some people just want to vent their emotions, but this obviously does not help to solve the actual problem. At least when it comes to which language to use in a project, or which language to use to break into the programming world, loud voices obviously don't help make the right choice.
Let's go back to the discussion of adults and see how Rust and Go have strengths and weaknesses under rational analysis.
Go vs. Rust: Performance
As mentioned earlier, programs generated by Go and Rust are both fast because they are compiled to native machine code without going through an interpreter or a virtual machine. But Rust's performance is still better, even comparable to C and C++, which are known as the industry's performance benchmarks. And unlike these old-fashioned languages, Rust also provides memory safety and concurrency safety mechanisms, while hardly affecting execution speed. Rust also allows developers to build complex abstractions without incurring a performance penalty at runtime.
In contrast, Although the performance of Go programs is also good, the design focus is mainly on the speed of development (including compilation), rather than the degree of execution. Go programmers prefer clear and readable code, so the running speed is a bit slower.
The Go compiler also doesn't spend much time generating the most efficient machine code, it's more concerned with compiling large amounts of code quickly. **So in runtime benchmarks, it is often the Rust program that overwhelms the Go program. **
Rust's runtime performance is also very consistent and predictable because it doesn't use garbage collection. Go's garbage collector is very efficient and is optimized to keep pause times as short as possible (pause times get shorter with each release of Go). But anyway, garbage collection always introduces some unpredictability in the way the program behaves, and this can be serious or even completely unacceptable for certain applications (such as embedded systems).
Because Rust's goal is to give the programmer complete control over the underlying hardware, **Rust programs can be deeply optimized to approach the maximum theoretical performance of the machine. **This makes Rust the best choice for certain use cases where speed of execution trumps all else, such as game programming, operating system kernels, web browser components, and real-time control systems.
It doesn't matter how powerful a programming language is if it's too difficult to learn and keeps most people out. Go seems to be deliberately designed to distinguish itself from languages of increasing complexity such as C++: **It has very little syntax, very few keywords, and not even many functions. **
This means that the Go language is easy to learn, and you can write various programs with it after a little understanding.
The key point here is "simplicity" three words. Of course, simple does not mean easy. But a small and simple language is definitely easier to learn than a large and complex language. There aren't many ways to achieve one effect, so high-quality Go code almost always looks the same. This has another benefit: we can quickly understand what a service with which we are not familiar is doing.
**Go's core ontology is small, but the standard library is very powerful. **That is, in addition to the Go syntax, our learning curve must also take into account this part of the standard library.
On the other hand, transferring functions from the language to the standard library means that everyone only needs to focus on learning the libraries related to the current development needs. **Go is also designed with full consideration of the needs of large-scale software development, and can strongly support large code bases and development teams. **In this type of scenario, new developers must be able to get started quickly. To this end, the Go community has always prioritized simplicity, clarity, versatility, and directness of programs.
**Rust is specifically designed to include a variety of powerful and useful features that help programmers do more with less code. **For example, Rust's match function makes it possible to quickly write flexible and expressive logic:
But also because Rust has a lot of design considerations, it is more difficult to learn, especially in the initial stage. But that's okay, C++ or Java has a lot to learn after all, and it doesn't even offer Rust's advanced features like memory safety. So it is really unreasonable to criticize Rust's too complicated sound: it is designed to emphasize expression and rich functions, we can't expect it to be so simple and pure while taking advantage of the benefits. So of course Rust has its own learning curve. But as long as this difficulty is overcome, there will be a smooth road ahead.
**Although Rust and Go borrow some features from each other (such as generics), it is fair to say that Rust's features are still superior, and Go's features are relatively lacking. **
Most languages provide some form of support for concurrent programming (i.e. performing multiple operations at the same time), but Go was designed for this from the ground up. **Go does not use operating system threads, but provides a lightweight alternative: goroutines. **Each goroutine is an independently executing Go function that the Go scheduler maps to one of the operating system threads under control. That is, the scheduler can manage a large number of concurrent goroutines very efficiently, while only using a limited number of operating system threads.
Therefore, we can run millions of concurrent goroutines in a single program without worrying about causing serious performance problems. Because of this, **Go is a complete solution for large-scale concurrent application scenarios such as web servers and microservices. **
Go also provides channels for goroutines, which is a fast, safe, and efficient way to communicate and share data. Go's concurrency design level is indeed very high, and the usage experience is quite relaxed and pleasant.
In general, the design of concurrent programs is very difficult, and building reliable and correct concurrent programs in any language is by no means easy. However, since this requirement was considered at the beginning of the project, the concurrent programming mechanism in Go has been made as simple as possible and well integrated.
In contrast, the concurrency mechanism in Rust has just landed and has not yet been stabilized, so everyone is welcome to continue to pay attention to this active development direction. This is also beneficial. For example, Rust's rayon library provides a very elegant and lightweight method that can convert sequential calculations into parallel calculations.
While it may not be easy to implement concurrent programs in Rust, it's still perfectly doable, and** these programs also benefit from Rust's carefully designed memory safety guarantees. **Take the Mutex class of the standard library as an example: in Go, we may forget to acquire the mutex before accessing something; but in Rust, there is no need to worry about it at all.
As mentioned earlier, both Go and Rust have their own ways to prevent various common programming mistakes, especially problems related to memory management. But **Rust goes further, it can be said that it spares no effort to ensure that everyone does not make unexpected security mistakes. **
That said, the programming experience in Rust is different than almost any other language, and can be quite challenging when first introduced. But in the eyes of many developers, this effort is clearly worth the money.
Many languages, including Go, also provide tools to help programmers avoid mistakes, but Rust takes this effect to a new level. Many incorrect programs don't even compile at all.
"Fighting the borrow checker" is a must for Rust newcomers, but for the most part, the checker isn't really the enemy. The problems it finds are indeed real bugs (or at least potential bugs) in the code. It may force us to fundamentally refactor our programs to avoid such problems - If you really put correctness and reliability as your top priority, then this strict requirement is obviously a good thing.
From another perspective, can a new language that does not change the programming method be called a new language? And when using other languages, Rust teaches us to think safely, which is equally important.
The actual difficulty of getting started with the Rust programming model may depend on what other languages you have used before. Python or Ruby programmers might find Rust too restrictive, but others might find the clarity and unambiguous constraints nice.
When you're solving a problem alone or in a small team, whether you choose a simple language or a rich language is purely a matter of personal preference. However, with the expansion of software scale, complexity, and team expansion, the differences between the two languages began to really emerge.
For large applications and distributed systems, the speed of code execution is often less important than the speed of development: a language like Go that deliberately emphasizes lean design can short the adaptation time of new developers and allow them to participate more quickly To contribute to large codebases.
Clarity and legibility are always more important than elegance when it comes to large-scale software development. **Go's limitations actually make it more adaptable to the needs of enterprises and large organizations than more complex and powerful languages such as Rust. **
Rust vs. Go: Differences
While both Rust and Go are popular and widely used modern languages, they're not really competitors because they're targeting arguably completely different use cases**.
The whole approach to programming in Go is completely different from Rust, and these features are particularly suitable for some people, but on the other hand can completely irritate others. Which makes sense, because if Rust and Go are both solving basically the same problems in basically similar ways, why do we need two separate languages?
So, can we start with the approaches taken by Rust and Go and interpret their respective essences? Let's try it out together.
"To garbage collect, or not to garbage collect" is always a question with no right answer. In general, garbage collection and automatic memory management can help us develop reliable and efficient programs quickly and easily. So for some developers, these are essential features.
Others argue that garbage collection, with its performance overhead and global pauses, can lead to unpredictable runtime behavior and introduce unacceptable latency. Of course there is truth to this statement.
The history of computer programming can be said to be an increasingly complex abstract development process. It allows programmers to solve problems without paying too much attention to how the underlying hardware actually operates. This design makes programs easier to write and more portable. But for other programs, access to hardware and precise control over how the program executes is more important.
**Rust's goal is to allow programmers to "close to the hardware" and regain more control; while Go abstracts away the architectural details, allowing programmers to get closer to the problem. **
In fact, for most programs, performance is less important than code readability. But if some projects really put performance first, there are many design trade-offs in **Rust that will help you push the execution speed of your code all the way to the limit. **
In contrast, Go cares more about code simplicity, and is even willing to sacrifice some runtime performance for it. But Go's build speed is unparalleled, which is often even more important for large-scale code projects.
On the other hand, if you don't force the program to never go wrong, the trade-offs will be different. Most code is not designed for long-term use, but some programs do run in production for years. Faced with these realities, it may be worth investing a little extra time in developing and ensuring that the program works correctly and reliably without incurring a heavy maintenance burden in the future.
Both Go and Rust help you write correct programs, but in different ways: **Go provides an excellent built-in testing framework, while Rust focuses on eliminating runtime bugs through borrow checkers. **
While both Go and Rust are good enough for serious development projects, it's best to fully understand their various features and advantages. In short, it doesn't matter what other people think: **Only you can decide which programming language is more suitable for your team and project needs. **
Conclusion I hope this article can help you understand the respective highlights of Rust and Go. If possible, you'd better experience these two languages a little bit, because they are very useful in any technical path, even for amateur programming enthusiasts.
But if you only spend your time studying one language seriously, please make sure of the respective expertise and inclinations of Go and Rust before making a choice.
Of course, knowledge of programming languages is only a small part of what makes a successful software engineer. In addition to programming, engineers have to be proficient in design, engineering, architecture, communication and collaboration. As long as everyone can do the following things well, no matter which programming language you choose, you will become an excellent software engineering master.