前言
前段时间由于项目需求短期内上手了 Rust,也顺便逛了一圈 Rust 的生态,这里记录一下学习的经验。
什么是 Rust?
一门编程语言, memory safe without garbage collection, fearless concurrency without data race
Technology from the past come to save the future from itself. - Graydon Hoare (Rust 创始人)
和 Go 一样,我觉得它们都很现代。
如果听腻了 Performance
, Reliability
, Productivity
,我告诉你一个我在 Rust 上学到的新词:
Ergonomic
Ergonomics is a measure of the friction you experience when trying to get things done with a tool.
Doing so is a clear ergonomic win.
This proposal will improve the ergonomics of the Rust language for development of command-line tools and "back end" / "computational kernel" programs.
Better ergonomics for pattern-matching on references.
Add async & await syntaxes to make it more ergonomic to write code manipulating futures.
The main motivation for this RFC is improving readability, ergonomics, and reducing paper cuts.
axum
is a web application framework that focuses on ergonomics and modularity.
async fn(Request) -> Result<Response, Error>
Tower is a library of modular and reusable components for building robust networking clients and servers.
Rust makes systems programming accessible. - Kosinix
Rust 入门
入门的学习资源不宜过多,有如下三个就足够了:
- rustlings
Jedi YounglingsSmall exercises to get you used to reading and writing Rust code.
- The Book
The Rust Programming Language
- Rust by Example
Learn Rust with examples
让自己对这门语言关键的概念有个印象,避免刻意套用以往的编程经验,试着去认识 Rust 的独特之处,入个门没
那么难。把 rustlings
的全部小练习完成后,就可以算是初步入门了,下面是我通关时出现的彩蛋 1。
🎉 All exercises completed! 🎉
+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
\\/
▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒
▒▒▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒
▒▒▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒
░░▒▒▒▒░░▒▒ ▒▒ ▒▒ ▒▒ ▒▒░░▒▒▒▒
▓▓▓▓▓▓▓▓ ▓▓ ▓▓██ ▓▓ ▓▓██ ▓▓ ▓▓▓▓▓▓▓▓
▒▒▒▒ ▒▒ ████ ▒▒ ████ ▒▒░░ ▒▒▒▒
▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒
▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▒▒▓▓▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒██▒▒▒▒▒▒██▒▒▒▒▒▒▒▒▒▒
▒▒ ▒▒▒▒▒▒▒▒▒▒██████▒▒▒▒▒▒▒▒▒▒ ▒▒
▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒
▒▒ ▒▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒ ▒▒
▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒
▒▒ ▒▒ ▒▒ ▒▒
We hope you enjoyed learning about the various aspects of Rust!
If you noticed any issues, please don't hesitate to report them to our repo.
You can also contribute your own exercises to help the greater community!
Before reporting an issue or contributing, please read our guidelines:
https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md
Rust 开发环境
-
Language Server 支持: rust-analyzer
{
"[rust]": {
"editor.formatOnSave": true
},
"rust-analyzer.check.command": "clippy"
}
-
依赖安全:cargo-audit, cargo-outdated
Rust 实际开发
这个阶段是最难熬的,除了善用搜索引擎 和 Stack Overflow,这里再增加一些参考资源:
- std: Rust 标准库
- crates.io 和 docs.rs: 官方软件仓库及其在线文档
- api guidelines: Rust API 风格指南
- cheats.rs: Rust 速查表
适应 Rust 特有的 Ownership
和 Borrow Checker
需要时间去磨合,根据项目需求着手一些实际的功能,模
仿和延伸接触到的 demo 或例子,关注数据结构、模块划分、错误处理、并发模型和设计模式,花上点心思不用多
久就能上手 Rust 开发。
Rust 生态背景
我一直比较排斥孤立地看待一门编程语言,了解这门语言所围绕的生态,认识它的历史背景和演进过程,这对你长 期的技术成长大有裨益。比如你可能不知道 Rust 最初也支持 M:N (hybrid) threading model, 以及 Go 语言和 Plan 9 之间惊人的联系。
下面是我推荐的一些信息来源:
Rust 进阶
不管最终能达到什么深度,只要是坚持涉猎都值得被认可:
- Effective Rust
- 源码阅读: rust, tokio/rayon, axum/tonic, tracing/opentelemetry
- reference: Rust 语言参考
- nomicon:
The Dark Arts of Advanced and Unsafe Rust Programming
- 网络协议:TCP, UDP
写在最后
技术圈里没有事物是完美的,Rust 同样也不是 Silver Bullet —— Safe Rust 并不能避免死锁、竞争条件、内存泄漏,而且 Unsafe Rust 的可靠性依赖于人为约束,Sync Rust 和 Async Rust 分化的生态具有迷惑性,但很多技术都得看场景和环境,不要陷入无谓的语言之争。
我用一句古文结束这篇博客:
“它山之石,可以攻玉”
Rust 吉祥物 Ferris
TOML 文件格式支持, 可选
往期也可以回顾,里面有不少精华内容