{{tag>[cli rust rustfmt formatter]}} ====== rustfmt / cargo fmt ====== Rust source를 표준 style로 format하고 CI에서 format drift를 검사하는 official component. ===== Installation ===== rustup의 default profile에는 보통 포함된다. 빠져 있다면 현재 toolchain에 component를 추가한다. rustup component add rustfmt rustfmt --version ===== Usage ===== cargo fmt [options] [-- rustfmt-options] rustfmt [options] file... * ''**cargo** fmt [options...]'' * ''**rustfmt** [options...] FILE...'' ===== Examples ===== cargo fmt cargo fmt --all cargo fmt --all -- --check rustfmt src/main.rs rustfmt --edition 2024 src/main.rs ===== Config ===== project 또는 상위 directory에 ''rustfmt.toml''이나 ''.rustfmt.toml''을 둔다. edition = "2024" max_width = 100 newline_style = "Auto" use_small_heuristics = "Default" stable toolchain에서 사용할 config는 stable option만 사용한다. nightly-only option을 공유 config에 넣으면 CI와 contributor 환경에서 실패할 수 있다. ===== CI ===== cargo fmt --all -- --check ''--check''는 file을 수정하지 않고 차이가 있으면 non-zero exit status를 반환하므로 CI에 적합하다. ===== Troubleshooting ===== * toolchain마다 rustfmt component가 별도이므로 ''cargo +nightly fmt''가 실패하면 해당 toolchain에 component를 추가한다. * generated source는 formatter 대상과 source control 정책을 명확히 구분한다. * edition mismatch가 있으면 ''Cargo.toml''의 package edition과 rustfmt config를 확인한다. ===== Help ===== +++ cargo fmt --help | 로컬 실행 파일이 없어 live output을 수집하지 않았다. cargo fmt --help rustfmt --help rustfmt --print-config default /tmp/rustfmt.toml +++ ===== See Also ===== * [[rust:ko]] * [[rust:cargo:ko]] * [[rust:clippy:ko]] * [[https://rust-lang.github.io/rustfmt/|Rustfmt Configuration]] ===== History ===== * codex:: 2026-07-20 Added rustfmt installation, workspace formatting, configuration, and CI checks. {{indexmenu>.#1|js}}