rustfmt.toml
Overview
This file, rustfmt.toml, serves as a configuration file for Rust's code formatter tool, rustfmt. It defines formatting rules and style preferences that rustfmt applies when formatting Rust source code. The file enables consistent code style across a project by specifying how the code should be formatted, including newline characters, import grouping, comment normalization, and usage of language features.
The configuration options set within this file customize the behavior of rustfmt to enforce particular style guidelines and formatting conventions, which helps maintain code readability and uniformity.
Configuration Options and Their Effects
Each entry in this TOML file corresponds to a specific rustfmt configuration option. Below is a detailed explanation of the included options:
1. newline_style = "Unix"
Purpose: Enforces the use of Unix-style newlines (
\n) instead of Windows-style (\r\n).Effect: Ensures consistent newline characters across different environments and contributors.
Usage: This is useful when contributors may work on different operating systems but the project requires a consistent newline style.
2. use_field_init_shorthand = true
Purpose: Requires the use of the shorthand syntax when initializing struct fields from local variables with the same name.
Effect: Instead of writing
StructName { field: field }, it enforcesStructName { field }.Usage Example:
let field = 5; let s = StructName { field }; // shorthand enforced
3. use_try_shorthand = true
Purpose: Enables the usage of the
?operator for error handling instead of the deprecatedtry!macro.Effect: The code formatter will prefer and enforce the use of the
?shorthand to improve readability and modernize the codebase.Note: The comment references that the
?operator was unstable in older Rust versions, but this configuration assumes stable usage.
4. use_small_heuristics = "Max"
Purpose: Sets the heuristic for line width and formatting to "Max".
Effect: This setting uses the 100-character line width limit for formatting decisions, as opposed to a more conservative default.
Reference: See the official
use_small_heuristicsdocumentation for detailed behavior.Benefit: Allows more code on a single line where possible, rather than breaking lines prematurely.
5. style_edition = "2021"
Purpose: Specifies the Rust edition that the formatter should target.
Effect: Adapts formatting rules to the Rust 2021 edition, which may affect certain syntax or conventions.
Usage: Ensures compatibility and idiomatic formatting according to the selected edition.
6. unstable_features = true
Purpose: Enables formatting features in
rustfmtthat are considered unstable or experimental.Effect: Allows usage of bleeding-edge formatting options that are not yet stabilized.
Caution: May cause unexpected behavior if used with incompatible
rustfmtversions.
7. imports_granularity = "Item"
Purpose: Controls how imports are grouped.
Effect: Imports are grouped at the item level, meaning each imported item appears separately rather than grouped in braces.
Example:
use std::io; use std::fs;rather than
use std::{io, fs};
8. group_imports = "StdExternalCrate"
Purpose: Groups imports into three categories: standard library, external crates, and the local crate.
Effect: Improves readability by organizing imports logically.
9. normalize_comments = true
Purpose: Normalizes comment formatting.
Effect: Adjusts comment spacing and alignment to a consistent style.
10. reorder_impl_items = true
Purpose: Enables automatic reordering of items inside
implblocks.Effect: Enforces a consistent order of methods and associated functions within implementations.
11. reorder_modules = true
Purpose: Automatically reorders module declarations.
Effect: Maintains a consistent order of module definitions, typically alphabetical.
12. wrap_comments = false
Purpose: Disables automatic wrapping of comments.
Effect: Preserves the original line breaks in comments without inserting new ones.
13. format_code_in_doc_comments = true
Purpose: Enables formatting of Rust code blocks inside documentation comments.
Effect: Ensures that example code within
///or/** */comments is also formatted.
14. format_macro_bodies = true
Purpose: Enables formatting of macro bodies.
Effect: Applies formatting rules inside macro definitions to improve macro readability.
15. format_macro_matchers = true
Purpose: Enables formatting of macro matchers (the pattern part of macro rules).
Effect: Ensures consistent formatting of macro pattern matching arms.
Implementation Details and Algorithms
This file does not contain executable code but instead defines key-value pairs that configure the behavior of the rustfmt tool. The tool reads these settings and applies formatting algorithms accordingly.
The relevant algorithms affected include:
Line wrapping and breaking: Controlled by
use_small_heuristics,wrap_comments, and line width settings.Import grouping and reordering: Based on settings like
imports_granularityandgroup_imports.Comment normalization: Adjusts white space and alignment for comments.
Code block formatting in documentation and macros: Controlled by
format_code_in_doc_comments,format_macro_bodies, andformat_macro_matchers.
The file enforces modern Rust idioms such as shorthand field initialization and the ? operator for error handling, ensuring adherence to updated Rust language standards.
Interaction with Other Parts of the System
Rustfmt Tool: This configuration file is directly consumed by the
rustfmtexecutable or library when formatting Rust source files in the project.Source Code Files: It affects how all Rust source files in the repository are formatted, ensuring consistency.
Build and CI Systems: Often integrated with continuous integration pipelines to enforce code style before merging.
IDE Plugins: Editors and IDEs that integrate
rustfmtuse this configuration to format code on save or on demand.
The rustfmt.toml file acts as a centralized style guide that complements Rust language features and coding conventions defined elsewhere in the system.
Visual Diagram
flowchart TD
A[rustfmt.toml Configuration] --> B[Newline Style]
A --> C[Field Init Shorthand]
A --> D[Try Shorthand]
A --> E[Line Width Heuristics]
A --> F[Rust Edition]
A --> G[Unstable Features]
A --> H[Import Grouping & Granularity]
A --> I[Comment Normalization]
A --> J[Reordering Impl Items]
A --> K[Reordering Modules]
A --> L[Comment Wrapping]
A --> M[Format Code in Doc Comments]
A --> N[Format Macro Bodies]
A --> O[Format Macro Matchers]
This flowchart depicts the main configuration areas defined in rustfmt.toml and their relationship as part of the overall formatting configuration.