Rust for Java Programmers
Rust 面向 Java 程序员
AI-driven guide: Written with GPT-5.4 assistance for experienced Java developers who want to learn Rust with clear conceptual mapping and practical migration advice.
AI 驱动说明:本书由 GPT-5.4 协助撰写,面向已经有 Java 经验、希望系统学习 Rust 的开发者。
This book is a bridge text. It assumes comfort with Java, Maven or Gradle, the JVM, exceptions, interfaces, streams, and the usual enterprise toolkit. The goal is not to re-teach programming. The goal is to show which instincts transfer cleanly, which ones must change, and how to reach idiomatic Rust without dragging Java habits everywhere.
这本书定位成桥接教材。默认已经熟悉 Java、Maven 或 Gradle、JVM、异常、接口、Stream,以及常见企业开发工具。重点不是重复教授编程基础,而是说明哪些经验可以平移,哪些习惯必须调整,以及怎样走到更符合 Rust 社区习惯的写法。
Who This Book Is For
适合哪些读者
- Developers who already write Java for backend services, tooling, data pipelines, or libraries
已经用 Java 写后端服务、工具、数据处理程序或基础库的开发者。 - Teams evaluating Rust for performance-sensitive or safety-sensitive components
正在评估 Rust,用于性能敏感或安全敏感模块的团队。 - Readers who want a chapter order that moves from syntax and ownership into async, FFI, and migration strategy
希望按“语法与所有权 → async → FFI → 迁移策略”顺序系统推进的读者。
What You Will Learn
这本书会覆盖什么
- How Rust differs from Java in memory management, error handling, type modeling, and concurrency
Rust 在内存管理、错误处理、类型建模和并发方面与 Java 的核心差异。 - How to map Java concepts such as interfaces, records, streams,
Optional, andCompletableFutureinto Rust equivalents
如何把接口、record、Stream、Optional、CompletableFuture这些 Java 经验映射到 Rust 世界。 - How to structure real Rust projects with Cargo, crates, modules, testing, and common ecosystem tools
如何使用 Cargo、crate、模块、测试和常见生态工具来组织真实 Rust 项目。 - How to migrate gradually instead of attempting a reckless full rewrite
如何渐进式迁移,而不是头脑发热搞一次性重写。
Suggested Reading Order
建议阅读顺序
| Range 范围 | Focus 重点 | Outcome 阶段结果 |
|---|---|---|
| Chapters 1-4 第 1-4 章 | Motivation, setup, core syntax 动机、环境、基础语法 | Can read and write small Rust programs 能够读写小型 Rust 程序。 |
| Chapters 5-7 第 5-7 章 | Data modeling and ownership 数据建模与所有权 | Can explain moves, borrows, and Option能够讲清 move、borrow 和 Option。 |
| Chapters 8-10 第 8-10 章 | Project structure, errors, traits 工程结构、错误、trait | Can organize multi-file crates and design APIs 能够组织多文件 crate 并设计基础 API。 |
| Chapters 11-14 第 11-14 章 | Conversions, iterators, async, FFI, testing 转换、迭代器、async、FFI、测试 | Can build realistic services and tools 能够开始写接近真实项目的工具和服务。 |
| Chapters 15-17 第 15-17 章 | Migration, tooling, capstone 迁移、工具链、综合项目 | Can plan a Java-to-Rust adoption path 能够设计一条从 Java 过渡到 Rust 的采用方案。 |
Companion Books In This Repository
仓库里的配套教材
- Rust for C/C++ Programmers
适合需要和 C/C++ 心智做对照的读者。 - Rust for C# Programmers
适合同时关心托管运行时与 .NET 生态对照的读者。 - Rust for Python Programmers
适合还要和脚本生态做横向比较的读者。 - Async Rust: From Futures to Production
适合继续深挖异步模型与生产实践。 - Rust Patterns
适合进入高级模式与工程细节。
Table of Contents
目录
Part I — Foundations
第一部分:基础
- 1. Introduction and Motivation
1. 引言与动机 - 2. Getting Started
2. 快速开始 - 3. Built-in Types and Variables
3. 内置类型与变量 - 4. Control Flow
4. 控制流 - 5. Data Structures and Collections
5. 数据结构与集合 - 6. Enums and Pattern Matching
6. 枚举与模式匹配 - 7. Ownership and Borrowing
7. 所有权与借用 - 8. Crates and Modules
8. crate 与模块 - 9. Error Handling
9. 错误处理 - 10. Traits and Generics
10. Trait 与泛型 - 10.3 Object-Oriented Thinking in Rust
10.3 Rust 中的面向对象思维 - 11. From and Into Traits
11. From 与 Into Trait - 12. Closures and Iterators
12. 闭包与迭代器
Part II — Concurrency and Systems
第二部分:并发与系统
- 13. Concurrency
13. 并发 - 13.1 Async/Await Deep Dive
13.1 Async/Await 深入解析 - 14. Unsafe Rust and FFI
14. Unsafe Rust 与 FFI - 14.1 Testing
14.1 测试
Part III — Migration and Practice
第三部分:迁移与实践
- 15. Migration Patterns and Case Studies
15. 迁移模式与案例 - 15.1 Essential Crates for Java Developers
15.1 Java 开发者常用 crate - 15.2 Incremental Adoption Strategy
15.2 渐进式引入策略 - 15.3 Spring and Spring Boot Migration
15.3 Spring 与 Spring Boot 迁移 - 16. Best Practices and Reference
16. 最佳实践与参考 - 16.1 Performance Comparison and Migration
16.1 性能比较与迁移 - 16.2 Learning Path and Resources
16.2 学习路径与资源 - 16.3 Rust Tooling for Java Developers
16.3 面向 Java 开发者的 Rust 工具
Capstone
综合项目
- 17. Capstone Project: Migrate a Spring Boot User Service
17. 综合项目:迁移一个 Spring Boot 用户服务