{"id":28756801,"url":"https://github.com/wx-chevalier/rust-notes","last_synced_at":"2025-06-17T03:08:50.844Z","repository":{"id":105127473,"uuid":"261219842","full_name":"wx-chevalier/Rust-Notes","owner":"wx-chevalier","description":"Rust Series，语法基础、数据结构、并发编程、工程实践，常见的代码示例 \u0026 数据结构与算法","archived":false,"fork":false,"pushed_at":"2025-03-13T07:59:07.000Z","size":568,"stargazers_count":36,"open_issues_count":0,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-13T08:34:54.746Z","etag":null,"topics":["algorithms","datastructures","rust","rust-lang"],"latest_commit_sha":null,"homepage":"https://ng-tech.icu/books/rust-series","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wx-chevalier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-04T15:14:34.000Z","updated_at":"2025-03-13T07:59:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"e2e9d3e8-0c0b-4d66-b6b7-2d7611837e88","html_url":"https://github.com/wx-chevalier/Rust-Notes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/wx-chevalier/Rust-Notes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wx-chevalier%2FRust-Notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wx-chevalier%2FRust-Notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wx-chevalier%2FRust-Notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wx-chevalier%2FRust-Notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wx-chevalier","download_url":"https://codeload.github.com/wx-chevalier/Rust-Notes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wx-chevalier%2FRust-Notes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260281524,"owners_count":22985630,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","datastructures","rust","rust-lang"],"created_at":"2025-06-17T03:08:50.252Z","updated_at":"2025-06-17T03:08:50.835Z","avatar_url":"https://github.com/wx-chevalier.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url]\n[![Stargazers][stars-shield]][stars-url]\n[![Issues][issues-shield]][issues-url]\n[![license: CC BY-NC-SA 4.0](https://img.shields.io/badge/license-CC%20BY--NC--SA%204.0-lightgrey.svg)][license-url]\n\n\u003c!-- PROJECT LOGO --\u003e\n\u003cbr /\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/wx-chevalier/repo\"\u003e\n    \u003cimg src=\"https://assets.ng-tech.icu/item/header.svg\" alt=\"Logo\" style=\"width: 100vw;height: 400px\" /\u003e\n  \u003c/a\u003e\n\n  \u003cp align=\"center\"\u003e\n    \u003ca href=\"https://ng-tech.icu/books/Rust-Notes\"\u003e\u003cstrong\u003e在线阅读 \u003e\u003e \u003c/strong\u003e\u003c/a\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n    \u003ca href=\"https://github.com/wx-chevalier\"\u003e代码案例\u003c/a\u003e\n    ·\n       \u003ca href=\"https://github.com/wx-chevalier/Awesome-Lists\"\u003e参考资料\u003c/a\u003e\n\n  \u003c/p\u003e\n\u003c/p\u003e\n\n\u003c!-- ABOUT THE PROJECT --\u003e\n\n# Introduction | 前言\n\n当我们需要编写接近实时高性能，稳健，并有足够开发效率的大程序时，譬如数据库、交易系统、大型桌面应用等，往往会首先选择 C 或者 C++。这种情况下我们往往有以下考量：\n\n- 不需要自动垃圾回收(GC)，避免因为垃圾回收导致的程序中断，以及可能引发的错误或者麻烦。\n\n- 有成熟完善的基础组件库，以保证开发效率。这一点正是 C 的不足，其虽然能保证高性能，但却需要重复造轮子；而 C++ 虽然内置了标准库，但是其却缺乏统一、完善的包管理器。\n\n- 零开销抽象(Zero Cost Abstraction)，我们希望有合适的抽象来保证开发效率、可读与可维护性，但是这种抽象必须是没有运行时开销的。因此我们需要静态强类型多范式语言，编译器能够尽早地发现问题，并且在编译阶段即能自动地进行性能优化；譬如 C++ 中，编译器如果发现虚类 (Virtual Class) 没有真正被用到甚至会优化掉虚表 (Virtual Table)。\n\n借鉴 [How Stylo Brought Rust and Servo to Firefox](http://bholley.net/blog/2017/stylo.html) 一文中的阐述，Mozilla, Google, Apple, 以及 Microsoft 等优秀公司开发的大型的 C/C++ 应用中，错误与漏洞从未停止，我们需要的是一门安全、高效、可扩展的语言。作为新语言，Rust 没有太多历史的包袱；但是 Rust 也并非一蹴而就，而是近 30 年的编程语言理论研究和实际软件工程的经验的集大成者：\n\n- They borrowed Apple’s [C++ compiler backend](https://llvm.org/), which lets Rust match C++ in speed without reimplementing decades of platform-specific code generation optimizations.\n\n- They leaned on the existing corpus of research languages, which contained droves of well-vetted ideas that nonetheless hadn’t been or couldn’t be integrated into C++.\n\n- They included the _unsafe_ keyword - an escape hatch which, for an explicit section of code, allows programmers to override the safety checks and do anything they might do in C++. This allowed people to start building real things in Rust without waiting for the language to grow idiomatic support for each and every use case.\n\n- They built a convenient [package ecosystem](https://crates.io/), allowing the out-of-the-box capabilities of Rust to grow while the core language and standard library remained small.\n\nRust 是为工业应用而生，并不拘泥于遵循某个范式( Paradigm )，笔者认为其最核心的特性为 Ownership 与 Lifetime；能够在没有 GC 与 Runtime 的情况下，防止近乎所有的段错误，并且保证线程安全(prevents nearly all segfaults, and guarantees thread safety )。Rust 为每个引用与指针设置了 Lifetime，对象则不允许在同一时间有两个和两个以上的可变引用，并且在编译阶段即进行了内存分配(栈或者堆)；Rust 还提供了 Closure 等函数式编程语言的特性、编译时多态(Compile-time Polymorphism )、衍生的错误处理机制、灵活的模块系统等。从应用层面来看，Mozilla 本身就是 Web 领域的执牛耳者，无论是使用 Rust 开发 Node.js 插件，还是 [Rust 默认支持 WebAssembly](https://parg.co/UPo)，都能很好地弥补目前笔者在进行 Web 前端 / Electron 客户端 / Node.js 计算模块的一些性能缺失。\n\n## 背景\n\n任何一门新技术的兴起，都是为了解决一个问题。自操作系统诞生以来，系统级主流编程语言，从汇编语言到 C++，已经发展了近 50 个年头，但依然存在两个难题：\n\n很难编写内存安全的代码。\n\n很难编写线程安全的代码。\n\n这两个难题存在的本质原因是 C/C++属于类型不安全的语言，它们薄弱的内存管理机制导致了很多常见的漏洞。其实 20 世纪 80 年代也出现过非常优秀的语言，比如 Ada 语言。Ada 拥有诸多优秀的特性：可以在编译期进行类型检查、无 GC 式确定性内存管理、内置安全并发模型、无数据竞争、系统级硬实时编程等。但它的性能和同时期的 C/C++相比确实是有差距的。那个时代计算资源匮乏，大家追求的是性能。所以，大家都宁愿牺牲安全性来换取性能。这也是 C/C++得以普及的原因。\n\n“Rust”这个名字包含了 GH 对这门语言的预期。在自然界有一种叫作锈菌（Rust Fungi）的真菌，这种真菌寄生于植物中，引发病害，而且号称“本世纪最可怕的生态病害”之一。这种真菌的生命力非常顽强，其在生命周期内可以产生多达 5 种孢子类型，这 5 种生命形态还可以相互转化，如果用软件术语来描述这种特性，那就是“鲁棒性超强”。可以回想一下 Rust 的 Logo 形状，像不像一个细菌？Logo 上面有 5 个圆圈，也和锈菌这 5 种生命形态相对应，暗示 Rust 语言的鲁棒性也超强。Rust 也有铁锈的意思，暗合裸金属之意，代表其系统级编程语言属性，有直接操作底层硬件的能力。此外 Rust 在字形组合上也糅合了 Trust 和 Robust，暗示了信任与鲁棒性。\n\n# Nav | 关联导航\n\n# About | 关于\n\n\u003c!-- CONTRIBUTING --\u003e\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n\u003c!-- ACKNOWLEDGEMENTS --\u003e\n\n## Acknowledgements\n\n- [Awesome-Lists](https://github.com/wx-chevalier/Awesome-Lists): 📚 Guide to Galaxy, curated, worthy and up-to-date links/reading list for ITCS-Coding/Algorithm/SoftwareArchitecture/AI. 💫 ITCS-编程/算法/软件架构/人工智能等领域的文章/书籍/资料/项目链接精选。\n\n- [Awesome-CS-Books](https://github.com/wx-chevalier/Awesome-CS-Books): :books: Awesome CS Books/Series(.pdf by git lfs) Warehouse for Geeks, ProgrammingLanguage, SoftwareEngineering, Web, AI, ServerSideApplication, Infrastructure, FE etc. :dizzy: 优秀计算机科学与技术领域相关的书籍归档。\n\n## Copyright \u0026 More | 延伸阅读\n\n笔者所有文章遵循[知识共享 署名 - 非商业性使用 - 禁止演绎 4.0 国际许可协议](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh)，欢迎转载，尊重版权。您还可以前往 [NGTE Books](https://ng-tech.icu/books-gallery/) 主页浏览包含知识体系、编程语言、软件工程、模式与架构、Web 与大前端、服务端开发实践与工程架构、分布式基础架构、人工智能与深度学习、产品运营与创业等多类目的书籍列表：\n\n[![NGTE Books](https://s2.ax1x.com/2020/01/18/19uXtI.png)](https://ng-tech.icu/books-gallery/)\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\u003c!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --\u003e\n\n[contributors-shield]: https://img.shields.io/github/contributors/wx-chevalier/repo.svg?style=flat-square\n[contributors-url]: https://github.com/wx-chevalier/repo/graphs/contributors\n[forks-shield]: https://img.shields.io/github/forks/wx-chevalier/repo.svg?style=flat-square\n[forks-url]: https://github.com/wx-chevalier/repo/network/members\n[stars-shield]: https://img.shields.io/github/stars/wx-chevalier/repo.svg?style=flat-square\n[stars-url]: https://github.com/wx-chevalier/repo/stargazers\n[issues-shield]: https://img.shields.io/github/issues/wx-chevalier/repo.svg?style=flat-square\n[issues-url]: https://github.com/wx-chevalier/repo/issues\n[license-shield]: https://img.shields.io/github/license/wx-chevalier/repo.svg?style=flat-square\n[license-url]: https://github.com/wx-chevalier/repo/blob/master/LICENSE.txt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwx-chevalier%2Frust-notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwx-chevalier%2Frust-notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwx-chevalier%2Frust-notes/lists"}