Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Essential Crates for Java Developers
Java 开发者常用的 Rust crate

What you’ll learn: Which Rust crates map most naturally to familiar Java engineering needs, how to choose them without rebuilding the entire Spring universe, and which combinations make sense for services, tools, and libraries.
本章将学习: 哪些 Rust crate 最适合映射 Java 开发里熟悉的工程需求,怎样在不重造整套 Spring 世界的前提下做选择,以及服务、工具、库项目分别适合什么组合。

Difficulty: 🟡 Intermediate
难度: 🟡 中级

There is no perfect one-to-one mapping between Rust crates and Java libraries. Rust ecosystems are usually smaller, more focused, and less framework-centered. The useful question is not “what is the exact Rust version of library X?” but “which crate category solves the same engineering problem with Rust-style composition?”
Rust crate 和 Java 库之间通常不存在完美的一比一映射。Rust 生态一般更小、更聚焦,也没那么强的框架中心化倾向。真正有用的问题不是“某个 Java 库在 Rust 里一模一样的替身是谁”,而是“哪类 crate 用更符合 Rust 习惯的组合方式解决同一种工程问题”。

Practical Mapping Table
实用映射表

Java need
Java 需求
Typical Java choiceCommon Rust choice
JSON serialization
JSON 序列化
Jackson, Gsonserde, serde_json
HTTP client
HTTP 客户端
HttpClient, OkHttpreqwest
async runtime
异步运行时
CompletableFuture plus executorstokio
web framework
Web 框架
Spring MVC, Spring WebFlux, Javalinaxum, actix-web, warp
logging and observability
日志与可观测性
SLF4J, Logback, Micrometertracing, tracing-subscriber, metrics
configuration
配置
Spring config, Typesafe configconfig, figment
CLI parsing
命令行解析
picocliclap
database access
数据库访问
JDBC, JPA, jOOQsqlx, diesel, sea-orm
gRPC
gRPC
gRPC Javatonic
testing helpers
测试辅助
JUnit ecosystembuilt-in #[test], rstest, proptest

Starter Sets by Project Type
按项目类型推荐的起步组合

HTTP Service
HTTP 服务

[dependencies]
axum = "0.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
tower = "0.5"
tower-http = { version = "0.6", features = ["trace", "cors"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }

That bundle feels familiar to Spring Boot developers: routing, middleware, JSON, runtime, and structured logs.
这套组合对 Spring Boot 开发者来说会比较有熟悉感:路由、中间件、JSON、运行时、结构化日志,基本都齐了。

CLI or Internal Tool
命令行工具或内部工具

[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
toml = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }

That is often enough for the kind of command-line tools Java teams would previously build with picocli and a small utility stack.
对很多原本会用 picocli 加一小堆工具库去写的 Java 命令行程序来说,这套已经很够用了。

Selection Heuristics for Java Teams
Java 团队的 crate 选择原则

  • prefer crates with strong documentation, recent releases, and active issue triage
    优先挑文档扎实、版本更新正常、问题处理活跃的 crate。
  • choose libraries that compose well instead of frameworks that hide every decision
    优先选能自由组合的库,而不是把所有决定都包起来的大框架。
  • read feature flags before enabling full everywhere, because compile-time surface area matters more in Rust
    别一上来就全开 full,Rust 的编译面和依赖面比 Java 更值得细抠。
  • prefer explicit types and thin abstractions before introducing dependency-injection-like indirection
    先接受显式类型和薄抽象,再考虑类似依赖注入那种间接层。

Common Migration Patterns
常见迁移思路

From Spring Boot Thinking
从 Spring Boot 思维迁过来

Java teams often look for one dependency that supplies controllers, dependency injection, validation, config binding, metrics, and database access in a single package. Rust usually works better with a smaller kit:
Java 团队常常会下意识寻找一个依赖,最好把控制器、依赖注入、校验、配置绑定、指标、数据库访问全包了。Rust 往往更适合一套小而清楚的组合:

  • axum for routing and handlers
    axum 负责路由和处理函数。
  • tower or tower-http for middleware
    towertower-http 负责中间件。
  • serde for JSON and config shapes
    serde 负责 JSON 和配置结构。
  • sqlx for database access
    sqlx 负责数据库访问。
  • tracing for logs and spans
    tracing 负责日志和 span。

That stack is less magical than Spring Boot, but it is also easier to debug because each part stays visible.
这套东西没有 Spring Boot 那么“全自动”,但调试起来反而更清楚,因为每一层都摆在明面上。

From JPA Thinking
从 JPA 思维迁过来

Rust developers often start with sqlx because it keeps SQL explicit and checks queries more aggressively. Teams that want a more ORM-like experience can evaluate diesel or sea-orm, but the first migration usually goes smoother when the data layer stays close to SQL.
Rust 开发里,很多人一开始会先选 sqlx,因为它让 SQL 保持显式,而且对查询的检查更直接。想要更 ORM 一点的体验,也可以评估 dieselsea-orm;不过第一轮迁移通常还是让数据层贴近 SQL 会更顺。

Where Java Developers Commonly Overbuild
Java 开发者最容易搭过头的地方

  • recreating dependency injection containers before understanding ownership and constructor-based composition
    还没理解所有权和基于构造参数的组合,就急着重建依赖注入容器。
  • reaching for ORM-style abstraction before modeling the actual data flow
    还没把真实数据流建清楚,就先上 ORM 式大抽象。
  • assuming every cross-cutting concern needs a framework extension point
    总觉得每个横切问题都必须挂在框架扩展点上。
  • building custom platform layers before learning the standard Cargo workflow
    Cargo 的标准工作方式还没熟,就先自建平台层和包装层。

For most teams, these are the first crates worth mastering:
对大多数团队来说,先把下面这些用顺最值:

  • serde
  • tokio
  • axum or reqwest, depending on whether the project is server-side or client-side
    axumreqwest,取决于项目偏服务端还是客户端。
  • tracing
  • thiserror and anyhow
    thiserroranyhow
  • clap

Once those are comfortable, the rest of the ecosystem becomes much easier to evaluate without importing Java habits that Rust does not benefit from.
这些掌握之后,再去看更大的生态就容易多了,也更不容易把对 Rust 没好处的 Java 习惯一并带过来。