Rust Formatter

The .rustfmt.toml file located in the $HOME directory is a global configuration file for Rust’s code formatters, such as rustfmt. In this file, you can find how my Rust code is always formatted.

General settings

First, we are using the 2018 edition of Rust.

edition = "2018"

Put single-expression functions on a single line.

fn_single_line = true

Format string literals where necessary.

format_strings = true

Maximum width of each line

max_width = 80

Merge multiple imports into a single nested import.

merge_imports = true

Structs and Enums

The maximum length of enum variant having discriminant, that gets vertically aligned with others. Variants without discriminants would be ignored for the purpose of alignment.

Note that this is not how much whitespace is inserted, but instead the longest variant name that doesn’t get ignored when aligning.

enum_discrim_align_threshold = 20

The maximum diff of width between struct fields to be aligned with each other.

struct_field_align_threshold = 20

Reorder impl items. type and const are put first, then macros and methods.

reorder_impl_items = true

Comments

Convert /* */ comments to // comments where possible.

normalize_comments = true

Break comments to fit on the line.

wrap_comments = true

Report FIXME items in comments.

report_fixme = "Always"

Report TODO items in comments.

todo = "Always"

Documentation

Format code snippet included in doc comments.

format_code_in_doc_comments = true

Convert #![doc] and #[doc] attributes to //! and /// doc comments.

normalize_doc_attributes = true

Whitespace

Use tab characters for indentation, spaces for alignment.

hard_tabs = false

Number of spaces per tab.

tab_spaces = 4

I want newlines to always be Unix style.

newline_style = "Unix"