2023-09-18 16:45:14 +00:00
|
|
|
|
#+TITLE: Rust Formatter
|
2020-07-16 12:25:18 +00:00
|
|
|
|
#+setupfile: headers
|
2020-09-28 16:09:45 +00:00
|
|
|
|
#+PROPERTY: header-args:toml :mkdirp yes :tangle ~/.rustfmt.toml
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
* Rust Formatter
|
2021-02-04 13:43:09 +00:00
|
|
|
|
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.
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
** General settings
|
2020-11-13 14:18:43 +00:00
|
|
|
|
First, we are using the 2018 edition of Rust.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
edition = "2018"
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Put single-expression functions on a single line.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
fn_single_line = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Format string literals where necessary.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
format_strings = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Maximum width of each line
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
max_width = 80
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Merge multiple imports into a single nested import.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
merge_imports = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
** Structs and Enums
|
2023-12-10 14:09:07 +00:00
|
|
|
|
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.
|
2020-11-13 14:18:43 +00:00
|
|
|
|
|
2023-12-10 14:09:07 +00:00
|
|
|
|
Note that this is not how much whitespace is inserted, but instead the
|
|
|
|
|
longest variant name that doesn't get ignored when aligning.
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
enum_discrim_align_threshold = 20
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
|
|
|
|
The maximum diff of width between struct fields to be aligned with
|
|
|
|
|
each other.
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
struct_field_align_threshold = 20
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
|
|
|
|
Reorder =impl= items. =type= and =const= are put first, then macros and
|
|
|
|
|
methods.
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
reorder_impl_items = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
** Comments
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Convert ~/* */~ comments to ~//~ comments where possible.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
normalize_comments = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Break comments to fit on the line.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
wrap_comments = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Report ~FIXME~ items in comments.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
report_fixme = "Always"
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Report ~TODO~ items in comments.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
todo = "Always"
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
** Documentation
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Format code snippet included in doc comments.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
format_code_in_doc_comments = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Convert ~#![doc]~ and ~#[doc]~ attributes to ~//!~ and ~///~ doc comments.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
normalize_doc_attributes = true
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2020-02-09 18:59:38 +00:00
|
|
|
|
|
2023-09-18 16:45:14 +00:00
|
|
|
|
** Whitespace
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Use tab characters for indentation, spaces for alignment.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
hard_tabs = false
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
Number of spaces per tab.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
tab_spaces = 4
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|
2023-12-10 14:09:07 +00:00
|
|
|
|
|
2020-11-13 14:18:43 +00:00
|
|
|
|
I want newlines to always be Unix style.
|
|
|
|
|
#+BEGIN_SRC toml
|
2021-10-12 09:31:20 +00:00
|
|
|
|
newline_style = "Unix"
|
2020-11-13 14:18:43 +00:00
|
|
|
|
#+END_SRC
|