当前位置:   article > 正文

Rust和Go语言在2024年的对比_go和rust怎么选择

go和rust怎么选择

Which is better, Rust or Go? Which language should you choose for your next project and why? How do they compare in terms of performance, simplicity, security, features, scalability, and concurrency? What do they have in common and what are their fundamental differences? Let’s find the answer through a friendly and fair comparison of Rust and Go.
Rust 和 Go 哪个更好?您应该为下一个项目选择哪种语言?为什么?它们在性能、简单性、安全性、功能、可扩展性和并发性方面如何比较?它们有什么共同点以及它们的根本区别是什么?让我们通过对 Rust 和 Go 的友好、公正的比较来找到答案。

一、Rust and Go are both great 都很棒

Firstly, it is very important to note that Go and Rust are both absolutely excellent programming languages. They are modern, powerful, widely adopted, and provide excellent performance.
首先,需要注意的是,Go 和 Rust 都是绝对优秀的编程语言。它们现代化、功能强大、被广泛采用,并提供卓越的性能。

Rust is a low-level statically typed multi-paradigm programming language that focuses on safety and performance — Gints Dreimanis [2]
Rust 是一种低级静态类型多范式编程语言,专注于安全性和性能 — Gints Dreimanis [2]

However: 然而:

Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software golang.org [3]
Go 是一种开源编程语言,可以轻松构建简单、可靠、高效的软件 golang.org [3]

In this article, I will try to briefly outline which scenarios I think Go is the ideal choice and which scenarios Rust may be a better choice.
在这篇文章中,我将尝试简要概述哪些场景我认为 Go 是理想的选择,哪些场景 Rust 可能是更好的选择。

二、Similarities 相似之处

What is the common goal of the two languages?
两种语言的共同目标是什么?

2.1 Memory safety 内存安全

Historically, one of the biggest causes of software errors and security bugs is unsafe or incorrect access to memory.
从历史上看,软件错误和安全漏洞的最大原因之一是对内存的不安全或不正确访问。

Rust and Go handle this problem in different ways, but both aim to be smarter and safer in managing memory than other languages.
Rust 和 Go 以不同的方式处理这个问题,但两者的目标都是比其他语言更智能、更安全地管理内存。

2.2 Fast and compact executable files快速且紧凑的可执行文件

They are all compile languages, which means your program will be directly compiled into executable machine code so that you can deploy the program as a single binary file. Compared with interpreted languages such as Python or Ruby, this also makes Rust and Go programs have extremely fast execution speed.
它们都是编译语言,这意味着您的程序将直接编译成可执行的机器代码,以便您可以将程序部署为单个二进制文件。与Python或Ruby等解释性语言相比,这也使得Rust和Go程序具有极快的执行速度。

2.3 Common language 共同语言

Rust and Go are both powerful and extensible general-purpose programming languages that you can use to develop various modern software. Both have excellent standard libraries and thriving third-party ecosystems, as well as strong commercial support and a large user base.
Rust 和 Go 都是强大且可扩展的通用编程语言,可用于开发各种现代软件。两者都拥有优秀的标准库和蓬勃发展的第三方生态系统,以及强大的商业支持和庞大的用户基础。

2.4 Pragmatic programming style 务实的编程风格

Although Go and Rust both have functional features related to functional and Object Oriented Programming (OOP), they are both pragmatic languages designed to solve problems in the most appropriate way.
尽管 Go 和 Rust 都具有与函数式和面向对象编程(OOP)相关的功能特性,但它们都是实用语言,旨在以最合适的方式解决问题。

2.5 Suitable for large-scale development 适合大规模开发

Rust and Go both have some useful features that make them suitable for large-scale programming, whether it is for large teams, large codebases, or both.
Rust 和 Go 都具有一些有用的功能,使它们适合大规模编程,无论是大型团队、大型代码库还是两者兼而有之。

For example, both Rust and Go use standard code formatting tools (Go’s gofmt, Rust’s rustfmt), which puts an end to useless arguments about the placement of parentheses.
例如,Rust 和 Go 都使用标准代码格式化工具(Go 的 gofmt、Rust 的 rustfmt),这杜绝了有关括号位置的无用争论。

Both also have excellent built-in high-performance standard build and dependency management tools; no more struggling with complex third-party build systems or learning a new one every few years.
两者还具有出色的内置高性能标准构建和依赖管理工具;不再需要与复杂的第三方构建系统作斗争,也不再每隔几年学习一个新系统。

三、Differences 差异

Although Rust and Go share many similarities, in some areas, rational people may prefer one language over the other to meet specific project needs.
尽管 Rust 和 Go 有许多相似之处,但在某些领域,理性的人可能会更喜欢一种语言而不是另一种语言来满足特定的项目需求。

3.1 Performance 性能

Go and Rust are both very fast. However, Go’s design is more conducive to fast compilation, while Rust is optimized for fast execution.
Go 和 Rust 都非常快。不过,Go 的设计更有利于快速编译,而 Rust 则针对快速执行进行了优化。

Rust’s runtime performance is also more consistent because it does not use garbage collection mechanism. On the other hand, Go’s garbage collector reduces some of the burden on programmers, making it easier to focus on solving the main problems rather than memory management details.
Rust 的运行时性能也更加一致,因为它不使用垃圾收集机制。另一方面,Go的垃圾收集器减轻了程序员的一些负担,使其更容易专注于解决主要问题而不是内存管理细节。

For areas where execution speed outweighs all other considerations (such as game programming, operating system kernels, web browser components, and real-time control systems), Rust is a better choice.
对于执行速度高于所有其他考虑因素的领域(例如游戏编程、操作系统内核、Web 浏览器组件和实时控制系统),Rust 是更好的选择。

3.2 Simple 简单

From a design perspective, Go is a small language with very few syntax, keywords, and language structures. You can quickly learn the basics of Go and use the language to improve work efficiency.
从设计角度来看,Go 是一种小型语言,语法、关键字和语言结构都非常少。您可以快速学习Go的基础知识并使用该语言提高工作效率。

This gives Go an advantage in projects with short time spans or teams that need to quickly introduce a large number of new programmers, especially when they are relatively inexperienced.
这使得 Go 在时间跨度短的项目或需要快速引入大量新程序员的团队中具有优势,尤其是当他们相对缺乏经验时。

3.3 Functional features 功能特点

On the other hand, Rust has almost all the features of programming languages you can imagine, as well as some features that you may not be able to imagine. This makes it a powerful and expressive language that can accomplish the same thing in multiple different ways.
另一方面,Rust 几乎拥有你能想象到的所有编程语言的特性,以及一些你可能想象不到的特性。这使其成为一种强大且富有表现力的语言,可以通过多种不同的方式完成同一件事。

If you’re transitioning to Rust from another language, you can probably find Rust equivalents for most of the features you’re used to. This gives Rust an advantage when large projects need to migrate from legacy languages like C++ or Java.
如果您从其他语言过渡到 Rust,您可能可以找到您习惯的大多数功能的 Rust 等效项。当大型项目需要从 C++ 或 Java 等遗留语言迁移时,这给 Rust 带来了优势。

3.4 Concurrency 并发

Unlike most languages, Go is designed with built-in support for concurrent programming, such as goroutines (a lightweight version of threads) and channels (a safe and efficient way to communicate data between concurrent tasks).
与大多数语言不同,Go 的设计内置了对并发编程的支持,例如 goroutine(线程的轻量级版本)和通道(一种在并发任务之间通信数据的安全有效的方式)。

These make Go the perfect choice for large-scale concurrent applications such as network servers and microservices.
这些使得 Go 成为网络服务器和微服务等大规模并发应用程序的完美选择。

3.5 Security 安全性

Rust is carefully designed to ensure that programmers cannot do unsafe things they do not want to do, such as overwriting shared variables. The compiler requires you to specify the way data is shared between different parts of the program and can detect many common errors and bugs.
Rust 经过精心设计,以确保程序员不能做他们不想做的不安全的事情,例如覆盖共享变量。编译器要求您指定在程序的不同部分之间共享数据的方式,并且可以检测许多常见的错误和错误。

Therefore, the so-called “battle with borrow checkers” is a common complaint among new Rust programmers. Implementing programs with safe Rust code often means fundamentally rethinking their design, which can be frustrating, but when reliability is your top priority, the benefits of doing so are worth it.
因此,所谓的“与借用检查器之战”是新 Rust 程序员的常见抱怨。使用安全的 Rust 代码实现程序通常意味着从根本上重新思考其设计,这可能会令人沮丧,但当可靠性是您的首要任务时,这样做的好处是值得的。

3.6 Scale 规模

Go aims to make it easy for you to expand projects and development teams. Its minimalist design brings a certain consistency, and the existence of a clearly defined standard style means that any Go programmer can read and understand new code libraries relatively quickly.
Go 旨在让您轻松扩展项目和开发团队。它的极简设计带来了一定的一致性,并且明确定义的标准风格的存在意味着任何Go程序员都可以相对快速地阅读和理解新的代码库。

When it comes to large-scale software development, clarity is better than intelligence. For large organizations, especially many distributed teams, Go is a good choice. Its fast build time is also conducive to rapid testing and deployment.
当涉及到大规模软件开发时,清晰度胜于智能。对于大型组织,尤其是很多分布式团队,Go 是一个不错的选择。其快速的构建时间也有利于快速测试和部署。

四、Trade-offs 权衡对比

The design teams of Rust and Go made some very different choices, so let’s take a look at some areas where these trade-offs make these two languages very different from each other.
Rust 和 Go 的设计团队做出了一些非常不同的选择,所以让我们来看看这些权衡使这两种语言彼此截然不同的一些领域。

4.1 Garbage collection 垃圾收集

Generally speaking, languages with garbage collection and automatic memory management functions (such as Go) can quickly and easily develop reliable and efficient programs, which is the most important for some people.
一般来说,具有垃圾收集和自动内存管理功能的语言(比如Go)可以快速、轻松地开发出可靠、高效的程序,这对某些人来说是最重要的。

However, garbage collection, due to its performance overhead and stop-the-world pauses, can make program behavior unpredictable at runtime, and some people find this inconsistency unacceptable.
然而,垃圾收集由于其性能开销和停止世界的暂停,可能会使程序在运行时的行为变得不可预测,有些人发现这种不一致是不可接受的。

Programmers must clarify that the language responsible for allocating and releasing each byte of memory (such as Rust) is more suitable for real-time or ultra-high-performance applications.
程序员必须澄清,负责分配和释放每个字节内存的语言(例如Rust)更适合实时或超高性能应用程序。

4.2 Abstract 摘要

The history of computer programming is an increasingly complex and abstract story that allows programmers to solve problems without worrying too much about how the underlying machines actually work.
计算机编程的历史是一个日益复杂和抽象的故事,它允许程序员解决问题而不必过多担心底层机器的实际工作方式。

This makes the program easier to write and possibly more portable. However, for many programs, accessing hardware and precisely controlling the execution of the program is more important.
这使得程序更容易编写并且可能更可移植。然而,对于许多程序来说,访问硬件并精确控制程序的执行更为重要。

Rust’s goal is to make programmers “closer to metal” and have more control, while Go abstracts architectural details and brings programmers closer to problems.
Rust 的目标是让程序员“更接近金属”并拥有更多的控制权,而 Go 抽象了架构细节,让程序员更接近问题。

4.3 Speed 速度

Rust has made many design trade-offs to achieve the best execution speed. In contrast, Go cares more about simplicity and is willing to sacrifice some (runtime) performance for it.
Rust 做出了许多设计权衡以实现最佳执行速度。相比之下,Go 更关心简单性,并愿意为此牺牲一些(运行时)性能。

At this point, whether you prefer Rust or Go depends on whether you are willing to spend more time waiting for the program to build or wait for the program to run.
此时,你更喜欢 Rust 还是 Go,取决于你愿意花更多的时间等待程序构建还是等待程序运行。

4.4 Correctness 正确性

Go and Rust both aim to help you write correct programs, but in different ways: for example, Go provides an excellent built-in unit test framework and a rich standard library, while Rust focuses on eliminating runtime errors using its borrow checker mechanism.
Go 和 Rust 都旨在帮助您编写正确的程序,但方式不同:例如,Go 提供了出色的内置单元测试框架和丰富的标准库,而 Rust 则专注于使用其借用检查器机制消除运行时错误。

To be fair, it is easier to write a given program in Go, but the result may contain errors more easily than the Rust version. Rust imposes disciplinary constraints on programmers, but Go lets programmers choose the level of discipline they want to apply to specific projects.
公平地说,用 Go 编写给定的程序更容易,但结果可能比 Rust 版本更容易包含错误。 Rust 对程序员施加了纪律约束,但 Go 让程序员选择他们想要应用于特定项目的纪律级别。

五、Conclusion 结论

hope this article can make you believe that both Rust and Go are worth considering seriously. You should reject the dilemma of this mistake: you can only learn one of them. In fact, the more languages you know, the higher your value as a software developer.
我希望这篇文章能让你相信 Rust 和 Go 都值得认真考虑。你应该拒绝这个错误的困境:你只能学习其中之一。事实上,您了解的语言越多,您作为软件开发人员的价值就越高。

Every new language you learn will give you a new way of thinking, which can only be a good thing. The most important factor for the quality and success of any software project is not the choice of language, but the skills of the programmer.
你学习的每一种新语言都会给你一种新的思维方式,这只能是一件好事。任何软件项目的质量和成功最重要的因素不是语言的选择,而是程序员的技能。

When using the language that suits you best, you become most proficient and can enjoy the most programming pleasure. Therefore, if the question is “Should I learn Rust or Go?”, the only correct answer is “Yes”.
当使用最适合你的语言时,你就会变得最熟练,并能享受到最大的编程乐趣。因此,如果问题是“我应该学习 Rust 还是 Go?”,唯一正确的答案是“是”。

References 参考

【1】《Rust vs Go in 2024》: Rust vs Go in 2024 — Bitfield Consulting

【2】Rust-lang.org:Rust Programming Language (rust-lang.org)

【3】Gints Dreimanis: Why You Should Use the Rust Programming Language

【4】 golang.org: The Go Programming Language

【5】rustcc.cn: Rust语言中文社区-首页 (rustcc.cn)

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/680893
推荐阅读
相关标签
  

闽ICP备14008679号