红穆笔记

rustfmt

格式化 Rust 源代�

100% 瀏覽器本地處理 0 數據上傳

常用示例

格式化文件,就地覆蓋原始文件

rustfmt {{路徑/到/源文件.rs}}

檢查文件的格式並在控制檯顯示所有變更

rustfmt --check {{路徑/到/源文件.rs}}

在格式化前備份所有修改的文件(原始文件會以 `.bk` 擴展名重命名)

rustfmt --backup {{路徑/到/源文件.rs}}

使用特定的 Rust 風格版次(格式化規則)以詳細模式格式化代碼

rustfmt --style-edition {{2015|2018|2021|2024}} {{[-v|--verbose]}} {{路徑/到/源文件1.rs 路徑/到/源文件2.rs ...}}

使用特定的 Rust 版次(語言特性和解析)格式化代碼

rustfmt --edition {{2015|2018|2021|2024}} {{路徑/到/源文件1.rs 路徑/到/源文件2.rs ...}}

提示:花括號{{…}}是需要替換的參數(路徑、文件名等);命令只在本機終端執行,本站不保存任何輸入。

英文原文對照

展開英文說明與示例(來源 tldr-pages)

Format Rust source code. More information

Format a file, overwriting the original file in-place

rustfmt {{path/to/source.rs}}

Check a file for formatting and display any changes on the console

rustfmt --check {{path/to/source.rs}}

Back up any modified files before formatting (the original file is renamed with a `.bk` extension)

rustfmt --backup {{path/to/source.rs}}

Format code using a specific Rust style edition (formatting rules) verbosely

rustfmt --style-edition {{2015|2018|2021|2024}} {{[-v|--verbose]}} {{path/to/source1.rs path/to/source2.rs ...}}

Format code using a specific Rust edition (language features and parsing)

rustfmt --edition {{2015|2018|2021|2024}} {{path/to/source1.rs path/to/source2.rs ...}}

命令信息

命令名rustfmt
用途格式化 Rust 源代�
適用平臺通用
示例數量5 條
官方文檔https://github.com/rust-lang/rustfmt
數據來源tldr-pages(CC-BY-4.0)· 社區中文翻譯

常見問題

rustfmt 命令是做什麼的?

格式化 Rust 源代�

rustfmt 怎麼用?

頁面按場景列出了5 條常用示例:每條示例先說明用途,再給出可直接複製的完整命令(花括號裏是需要替換的參數)。

rustfmt 最常用的操作是什麼?

格式化文件,就地覆蓋原始文件:rustfmt {{路徑/到/源文件.rs}}

命令裏的花括號是什麼意思?

花括號{{…}}表示需要替換成你自己的值,例如路徑、文件名;同名參數方括號(如 [-C|--directory])表示可選寫法。