windows服务器自建RustDesk服务
Rustfmt简单配置
- 1. Rustfmt概述
- 2. Rustfmt安装
- 3. Rustfmt使用
- 4. Rustfmt配置文件
1. Rustfmt概述
Rustfmt是用于格式化Rust代码的一款工具,我们可以通过修改配置文件让它根据我们想要的风格格式化代码。若想详细了解Rustfmt可以前往其github仓库。
2. Rustfmt安装
如果Rust版本大于等于1.24,你可以直接在命令行输入 rustfmt --version查看Rustfmt的版本 ,如果没有安装的话,可以输入以下命令安装你想要的版本。
Stable版本
rustup component add rustfmt # 安装 cargo fmt # 格式化当前工作目录下的cargo工程代码Nightly版本
rustup component add rustfmt --toolchain nightly cargo +nightly fmt # 用nightly版本的Rustfmt需要加上nightly如果想要从源码编译Rustfmt,请参阅其github仓库
3. Rustfmt使用
在命令行输入 rustfmt filename即可直接格式化对应的Rust源文件,也可以直接输入 cargo fmt格式化整个crate。
4. Rustfmt配置文件
Rustfmt是高度可配置的,你可以在cargo工程目录中或者任意层级的父文件夹中创建一个名为rustfmt.toml 或 .rustfmt.toml的文件,这个文件便是Rustfmt的配置文件,作用于其所在的当前文件夹以及所有子文件夹。
默认情况下,Rustfmt采用 Rust style guide中的风格。
以下是一些常见的配置
选项
默认值
可选值
Stable
说明
array_width
60
不大于max_width的数值
是
触发数组换行的宽度
binop_separator
"Front"
"Front"/"Back"
否
二元操作符换行时放前面还是放后面
blank_lines_lower_bound
0
unsigned int
否
item之间的最小空行
blank_lines_upper_bound
1
非负数
否
item之间的最大空行数
brace_style
"SameLineWhere"
"AlwaysNextLine", "PreferSameLine", "SameLineWhere"
否
大括号换行样式
chain_width
60
不大于max_width的正数
是
一条链式语句最大适应宽度
combine_control_expr
true
true/false
否
控制语句作为函数实参时是否紧随其后
comment_width
80
任何正数
否
注释的最大宽度,只有在 wrap_comments 为true时才有效
control_brace_style
"AlwaysSameLine"
"AlwaysNextLine", "AlwaysSameLine", "ClosingNextLine"
否
控制语句换行样式
edition
"2015"
"2015", "2018", "2021"
是
指定parser版本
empty_item_single_line
true
true/false
否
空item独占一行
fn_args_layout
"Tall"
"Compressed", "Tall", "Vertical"
是
函数参数展开样式
fn_call_width
60
不大于max_width的正数
是
函数调用的最大宽度
fn_single_line
false
true/false
否
单表达式函数占一行
format_code_in_doc_comments
false
true/false
否
格式化注释中的代码
format_macro_bodies
true
true/false
否
格式化宏里的代码
format_strings
false
true/false
否
必要时格式化字符串字面量
hard_tabs
false
true/false
是
使用tab缩进,空格对齐
ignore
-
-
否
跳过格式化的文件,使用如: ignore=["examples"]
imports_indent
"Block"
"Block", "Visual"
否
use语句缩进样式
imports_layout
"Mixed"
"Horizontal", "HorizontalVertical", "Mixed", "Vertical"
否
use语句样式
indent_style
"Block"
"Block", "Visual"
否
expression和item缩进样式
inline_attribute_width
0
正数
否
如果item和attribute宽度之和小于该值,则纳入同一行
match_arm_leading_pipes
"Never"
"Always", "Never", "Preserve"
是
match分支前是否包含 leading pipe
max_width
100
正数
是
每行的最大宽度
merge_derives
true
true/false
是
合并多行derives为一行
imports_granularity
"Preserve"
Preserve, Crate, Module, Item, One
否
use语句该如何合并或分割
newline_style
"Auto"
"Auto", "Native", "Unix", "Windows"
是
换行符
overflow_delimited_expr
false
true/false
否
当类似于struct、slices、arrays、和blocks/array-like的东西作为最后一个参数时,允许溢出而不是换行缩进
reorder_imports
true
true/false
是
根据字母顺序重排use语句
reorder_modules
true
true/false
是
根据字母顺序重排同一组modules
required_version
CARGO_PKG_VERSION
发布的工具版本
否
需要的最小版本
short_array_element_width_threshold
10
不大于max_width的正数
是
数组元素的宽度阈值被认为是“短”的的临界值
tab_spaces
4
正数
是
tab所占空格大小
where_single_line
false
true/false
否
强制 where 语句占一行
wrap_comments
true
true/false
否
将注释分开以适应该行
version
"One"
"One", "Two"
否
使用哪个版本的规则格式化代码。
笔者水平有限,如有错误,请直接指出!