首页/开发/代码质量与评审速查/rustfmt:Rust 代码格式化
规范与静态检查65 条

rustfmt:Rust 代码格式化

rustfmt 按官方风格重排 Rust 代码:通过 rustfmt.toml 调整少数稳定选项,多模块项目用 skip_children 或按文件就地忽略,cargo fmt 负责批量应用。

配置rustfmt.toml
常用项max_width / edition / use_small_heuristics
应用cargo fmt --all

配置与命令

配置与命令
max_width = 100
edition = "2021"

# 命令行
cargo fmt --all -- --check
CI 用 cargo fmt --check:只检查不修改,阻止未格式化代码合入。

易错点

  • 团队工具版本不一致:输出格式反复波动,diff 出现无关改动
  • 尝试启用 unstable 选项:Nightly 与稳定版行为不同,跨机器结果不一致

数据来源与参考

  • rustfmt 官方文档(rust-lang.github.io/rustfmt)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌