This repository contains Subtale's opinionated rustfmt configuration file (
.rustfmt.toml
).
The .rustfmt.toml
should be included in the root directory of your Rust project. This will ensure that rustfmt picks it up when formatting.
Below, you can find the reasoning behind all of the configuration options present in Subtale's custom rustfmt configuration.
Some of these settings are set to their default values; this is intentional to ensure that they are kept as default.
This is a required option because some of the below settings are considered "unstable" by rustfmt.
Separates control expression from their function calls:
fn example() {
// If
foo!(
if x {
foo();
} else {
bar();
}
);
// IfLet
foo!(
if let Some(..) = x {
foo();
} else {
bar();
}
);
}
Replaces consecutive underscore variables with a single ..
within tuple patterns:
let (lorem, ipsum, _, _) = (1, 2, 3, 4);
// becomes
let (lorem, ipsum, ..) = (1, 2, 3, 4);
Puts single-expression functions on a single line:
fn lorem() -> usize { 42 }
Formats code snippets in doc comments.
Formats metavariables in macro declarations:
macro_rules! foo {
($a:ident : $b:ty) => {
$a(42): $b;
};
($a:ident $b:ident $c:ident) => {
$a = $b + $c;
};
}
Reorganizes imports into three distinct groups:
std
,core
, andalloc
- external crates
self
,super
, andcrate
imports
use alloc::alloc::Layout;
use core::f32;
use std::sync::Arc;
use broker::database::PooledConnection;
use chrono::Utc;
use juniper::{FieldError, FieldResult};
use uuid::Uuid;
use super::schema::{Context, Payload};
use super::update::convert_publish_payload;
use crate::models::Event;
Forces uppercase hex literals.
Merges imports from the same crate into a single use
statement.
use foo::{
a, b,
b::{f, g},
c,
d::e,
};
use qux::{h, i};
Forces the style of items inside an import block.
use foo::{xxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz};
use foo::{
aaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbb,
cccccccccccccccccc,
dddddddddddddddddd,
eeeeeeeeeeeeeeeeee,
ffffffffffffffffff,
};
Puts a trailing comma after a block-based match arm:
match lorem {
Lorem::Ipsum => {
println!("ipsum");
},
Lorem::Dolor => println!("dolor"),
}
Converts /* */
comments to //
comments where possible.
Converts #[!doc = "..." ]
attributes to ///
comments where possible.
Reorders impl items. type
and const
are grouped together, then macros and functions:
impl Iterator for Dummy {
fn next(&mut self) -> Option<Self::Item> {
None
}
type Item = i32;
}
// becomes
impl Iterator for Dummy {
type Item = i32;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
Ensures that imports and extern crate statements are sorted alphabetically (in groups).
use dolor;
use ipsum;
use lorem;
use sit;
Uses the field init shorthand where possible:
fn main() {
let x = 1;
let y = 2;
let z = 3;
let a = Foo { x: x, y: y, z: z };
}
// becomes
fn main() {
let x = 1;
let y = 2;
let z = 3;
let a = Foo { x, y, z };
}
Replaces the use of the try!
macro with the ?
shorthand:
try!(ipsum.map(|dolor| dolor.sit()));
// becomes
ipsum.map(|dolor| dolor.sit())?;
Breaks comments to fit within the maximum line width (80 characters).