Rust Library Recommendation
异步运行时
- tokio: Rust 生态中最成熟的异步运行时库
- tokio-graceful-shutdown
日志、遥测
- tracing: tokio 开发的库,用于收集结构化的、基于事件的诊断信息。
tracing
负责提供日志 API - tracing-subscriber: 负责将日志转发到外部系统,比如 OpenTelemetry Collector、标准输出
- tracing-appender: 负责写文件的组件
- tracing-log:提供 log 到 tracing 的兼容
- tracing-flame: 生成火焰图
命令行参数
- clap: 命令行参数处理最成熟的组件
- clap-complete: 命令行补全工具
配置管理
错误处理
序列化与反序列化
- serde: Rust 生态中事实标准
ORM
- sea-orm:异步 ORM 库,最成熟
工具
- uuid: UUID 库
- rand: 随机数库
- roaring-rs: Roaring Bitmap 的 rust 实现
- grade:Validate 结构体
- chrono:时间处理
缓存
- sccache: Mozilla 开发的通用环境库,支持多种存储
大数据
后端网络服务
- axum: 虽然不是性能最好 [1],但成熟度、文档和开发体验绝对一流
- tower-http:中间件,和 axum 配合使用
- tower-governor:governor 限流中间件
- http:通用 http 协议相关
- utoipa:openapi 相关
- utoipa-swagger-ui:openapi 相关,swagger 界面
Help rust error handling
Reply from rickyman20:[2]
- Libraries should always use thiserror or something similar, but definitely never anyhow unless for user-passed functions (and even that is questionable)
- Binaries are usually better served by anyhow
- If you have a very complex binary, it's useful for use thiserror for error types within the modules of your binary, and then use anyhow in main specifically, so you can just ? every error and get them printing
Getting started with Tracing
The tracing crate provides the API we will use to emit traces. The tracing-subscriber crate provides some basic utilities for forwarding those traces to external listeners (e.g., stdout).[3]