在 Go 和 Rust 之间,如果你不知道学习哪一门语言,或想深入了解二者的差异,阅读该篇即可
一、Tradeoff 比较
Rust 和 Go 都是极其优秀的语言——文章观点绝大部分来自 Rust vs Go。本文属于文章摘录,关键的学习资源在最后章节。
Rust
- Performance 🚀
- Safety ⛑
- Expressiveness ✍️
- Productivity ✨
Go
- Simplicity 🤟🤲
- Productivity ✨
- Expressiveness ✍️
- Safety ⛑
- Performance 🚀
Expressiveness(表现力):对开发者友好,更少代码或更简洁代码完成更多事情
举例
Expressiveness
Rust 的 match 特性让写出来的代码简洁,而且逻辑更加灵活、有弹性和有表现力 👍。
func is_prime(num uint) -> bool {
match num {
0...1 => false,
_ => !(2...num).any(|d| => num % d == 0),
}
}
Go 没有 match 和 range 代码更多
func isPrime(number int) bool {
if number < 2 {
return false
}
for i := 2; i < number; i++ {
if number%i == 0 {
return false
}
}
return true
}
Safety
很多语言都试图在代码开发过程让程序员尽量避免编码错误,Rust 提供的工具让这一领域达到了新高度——故 Rust 更胜一筹。
"Fighting with the borrow checker" is a common syndrome for new Rust programmers
二、擅长领域比较
Rust
- 游戏开发 Game Programming
- 操作系统核心开发 Operating System Kernels
- 浏览器核心组件 Web Browser Components
- 实时控制系统 Real-time Control Systems(不能有 STW - 即 GC Stop The World,故 go 和 java 无法胜任)
- 嵌入式系统 embedded systems
Go
更适合企业级软件开发——因为 Go 能让初级工程师也能具备很高的生产力。
- web servers and microservices.
共同领域
比如高负载的 web service, 能够同时水平或垂直扩展。
三、性能比较
Rust 的 runtime 性能极佳,比 Go 略胜一筹,但是构建速度 Go 无人能敌。
性能论据
Rust 没有 GC,因为不会有 Go 的 Stop the world 的 GC 通病(GC 让运行时的行为难以预测,而且会引入无法接受的时延),Rust 运行时性能具备一致性和可预测性,因此 Rust 的性能更好。当然 Go 一直在优化其 GC 的性能,让每一次 stop-the-world 暂停时间更短,然而 GC 无可避免会给软件的运行时行为引入不可预测性,尤其在嵌入式系统这是很严肃的问题,很可能无法忍受。
unpredictably at run-time, and introduces unacceptable latency
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.
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 Simplicity over Performance
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.
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.
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.
代码正确性
Go 可以用来开发明天就要上线的代码(Go 强调开发速度),但是 Rust 编写出来的代码可以运行五年而无需担心要修复线上问题(Rust 强调代码正确性)。
论据
Rust Correctness
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
四、我的选择
我认为可读性比性能更重要,因为第一绝大部分情况下性能不再是选择语言的第一考虑,尤其是 Go 或 Rust 相比于 Java, C#, JavaScript, Python 等已经快出一大截,最后软件工程不是一个人的事而是团队合作——故可读性更重要,所以我选择学习 Go。
五、最重要的放最后
学习编程语言只是成为成功软件工程师很小的一部分而已。软件工程师最重要的应该是让自己具备设计、工程化、架构、沟通和合作。如果这些点全然掌握,你将会是一个优秀的软件工程师,而不必纠结是用哪一种语言。
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!
学习资源
Go
- Install Go
- Go tutorials by Bitfield
- For the Love of Go
- A Tour of Go
- Go By Example
- The Go Playground
- Awesome Go