목차

, , ,

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...

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

Help

+ cargo fmt --help

로컬 실행 파일이 없어 live output을 수집하지 않았다.
cargo fmt --help
rustfmt --help
rustfmt --print-config default /tmp/rustfmt.toml

+

See Also

History