A comment before ->#edition 2024
rustfmt drops the comment. apfmt keeps it in place.
rustfmt drops comments in some positions; apfmt keeps every comment.
rustfmt silently deletes comments in several positions, such as around ->, inside parenthesized types and bounds, and inside use declarations. apfmt keeps all of them, at or close to where they were written.
->#edition 2024rustfmt drops the comment. apfmt keeps it in place.
->#edition 2024rustfmt drops the comment. apfmt keeps it in place.
rustfmt drops the first two comments. apfmt keeps all three.
->#edition 2024rustfmt drops the comment. apfmt keeps it and puts -> u32 on an indented line.
rustfmt drops the comment. apfmt keeps it and breaks the parentheses.
rustfmt drops the comment. apfmt keeps it.
use declarations#edition 2024rustfmt drops the comments, and drops the last use entirely. apfmt keeps them in place.
; of a use#edition 2024rustfmt drops the comment. apfmt moves it after the ;.
apfmt leaves macro arguments unchanged when formatting would add or remove a space between punctuation characters; rustfmt reformats them.
A procedural macro can see whether two punctuation characters are written next to each other, and its custom syntax may read them as one operator, such as <-, <-> or --. apfmt formats macro arguments that parse as an expression only if every such pair stays as written. Otherwise it copies the arguments unchanged, just as it copies arguments that do not parse.
rustfmt formats them anyway (it only leaves x<-y alone), which can change what the macro does.
Commas, semicolons and the spacing after ? are not checked, because they are never part of an operator.
& & must not become &&#edition 2024.. .. keeps its spacing#edition 2024< < must not become <<#edition 2024When a macro body that cannot be parsed is reindented, apfmt leaves the text of its string literals alone; rustfmt shifts it and so changes the string.
A macro body that does not parse is copied and reindented with its statement. apfmt keeps the lines of a multi-line string literal exactly as they are. rustfmt shifts them along with the code, which changes the value of the string.
When the start of a chain spans several lines, apfmt puts a single remaining element right after the closing delimiter ().await?); rustfmt puts it on its own line.
When the first part of a chain (its root) spans several lines, the Style Guide joins the next element to the closing line and block-indents any others. Neither formatter follows this. With two or more remaining elements, apfmt does what rustfmt does: each element goes on its own line at the indentation of the root. They differ when only one element remains: apfmt puts it after the closing delimiter, and rustfmt puts it on its own line. If the element does not fit after the delimiter, apfmt puts it on its own line too.
In the head of an if let, a while let or a let chain, the line this saves lets apfmt move the whole expression to the line after =, as the Style Guide prefers: "prefer breaking after the = for any let sub-expression in an if or while expression that does not fit". rustfmt keeps the chain broken on the line of =. In a plain let statement, apfmt also keeps a multi-line expression on the line of =, unless moving it saves at least two lines.
match head#edition 2024if let, the saved line moves the expression after =#edition 2024apfmt adds a trailing comma to broken lists nested in macro arguments and removes it from lists on one line; rustfmt does neither.
A broken list (call arguments, struct literal fields, array elements) nested in macro arguments gets a trailing comma, as the Style Guide asks for any broken list. rustfmt omits it inside macro calls.
The last argument of the macro call itself gets no trailing comma in either formatter, because the macro might not accept one. A nested list is parsed as an expression, so a comma there is always valid.
A list on one line has no trailing comma (Style Guide). When a list in macro arguments has a trailing comma and fits on one line, rustfmt keeps the comma.
apfmt lays out attribute arguments like call arguments, always with a trailing comma when broken; rustfmt writes the comma only if the source has it.
The Style Guide says: "For attributes with argument lists, format like functions." apfmt does this for #[derive(...)], #[cfg(...)], #[cfg_attr(...)] and similar attributes, within the attr_fn_like_width limit: the arguments go on one line, or one per line with a trailing comma. Both formatters make one exception and fill a broken derive list with several names per line.
rustfmt differs in three ways:
cfg(all(a, b,))). apfmt removes both.sql_type = a::b), it leaves the attribute on one line as long as it fits within max_width. apfmt applies attr_fn_like_width here too.For attributes with argument lists, format like functions.
When the only argument does not fit on the line of the call, apfmt puts it on its own line if it fits there; rustfmt hugs it and breaks its contents.
Hugging means that the inner delimiters stay next to the outer ones and only the contents are broken, with outer(inner( on the first line and )) on the last. When the only argument of a call is another call or a tuple that does not fit on the line, rustfmt hugs it. apfmt puts the argument on its own indented line if it fits there, which takes fewer lines. The same applies to a tuple that is the only element of an array.
A one-element tuple (x,) as the only argument either hugs like any other delimited argument or goes on its own line. rustfmt instead breaks the chain inside it at its root and closes the tuple with ,) on the line of the last element.
When one part of an expression does not fit, apfmt still formats the rest; rustfmt often leaves the whole expression as written.
When a part of an expression cannot fit within the line width, rustfmt often gives up and leaves the whole expression as written, including its layout, indentation and punctuation. apfmt formats everything around the long part.
This also explains other differences in such code: rustfmt keeps details of the source there that it would normally change, such as the spacing of quote::quote!{.
match with a long arm#edition 2024macro_rules! transcribers are formatted where they parse #apfmt formats macro_rules! transcribers wherever they parse; rustfmt often leaves a macro_rules! as written.
apfmt formats a transcriber (the right-hand side of a rule) when it parses as items or statements once its metavariables are replaced. m!{ gets its space, a { $x } body breaks like a block, and calls and chains inside are laid out as anywhere else.
A transcriber that does not parse is copied and reindented with its rule, so it moves when the indentation of the macro_rules! changes. rustfmt often leaves a macro_rules! exactly as written, including its indentation: code indented with tab_spaces = 2 stays that way even under the default configuration.
vis metavariable#edition 2024If a struct literal field value cannot fit on the line of its name, apfmt moves it to the next line; rustfmt leaves the line too long.
max_width where that is possible.Both formatters keep a field value on the line of its name whenever it fits there, even if it has to be broken. When every layout on that line overflows, for example with a long string literal, rustfmt leaves the line too long, and apfmt moves the value to the next line, block-indented, where it fits. A cast is the exception: it breaks before as on the line of the name, like a binary operator.
Both align trailing comments, but apfmt leaves out items far wider than the rest and aligns in a few cases where rustfmt does not.
Both formatters align the trailing comments of list items on consecutive lines, although the Style Guide asks for a single space. rustfmt aligns them to the widest item, however far right that pushes the comments and even if a line then exceeds max_width.
apfmt leaves out items that are much wider than the others: at most a quarter of the items (or a single item), when more than trailing_comment_align_threshold columns (40 by default) separate them from the rest. It also leaves out lines that alignment would push past max_width. These lines keep a single space.
apfmt also aligns in three cases where rustfmt keeps a single space. Neither choice is clearly better: apfmt has one rule, and rustfmt changes less of the source.
{} #apfmt writes an empty block as {} everywhere; rustfmt splits it over two lines in some places.
apfmt writes an empty block as {} everywhere, including in if-else chains and after a broken signature or a where clause. rustfmt puts { and } on separate lines in those places. The Style Guide does not cover these cases.
if-else chain#edition 2024where clause#edition 2024= moves to the next line #apfmt moves a long string after = to the next line, including raw and wide strings; rustfmt sometimes leaves it overflowing.
When the right side of a let, a const or an assignment is a single string literal too long for the line of =, apfmt moves it to the next line, where it fits or overflows by fewer columns. rustfmt leaves it on the line of = when it is a raw string, when its text contains wide characters such as CJK (apfmt measures their display width), or when it does not fit on the next line either.
apfmt applies the Style Guide rule to all struct variants and only to them; rustfmt also reacts to tuple variants and skips some struct variants.
The Style Guide says: "In an enum with multiple struct variants, if any struct variant is written on multiple lines, use the multi-line formatting for all struct variants." apfmt applies this to every struct variant and to struct variants only. rustfmt differs in two ways:
In an enum with multiple struct variants, if any struct variant is written on multiple lines, use the multi-line formatting for all struct variants.
apfmt hugs a single closure or control-flow argument whenever its first line fits; rustfmt also looks at how wide its head is.
A single closure argument hugs the call whenever its first line fits within max_width. rustfmt puts the closure on its own line when its head is wide. The same applies to a single if, match, while or for argument with a long head, which rustfmt hugs only when the first line is within fn_call_width.
while with a long head#edition 2024apfmt puts each field of a broken struct pattern on its own line; rustfmt can put all fields on one indented line.
The Style Guide formats patterns like the corresponding expressions, so a struct pattern that does not fit within struct_lit_width has one field per line, with .. last and no comma. rustfmt puts all the fields on one indented line when they fit within struct_lit_width without .. but not with , .. added. With more fields, it also uses one per line.
, ..#edition 2024apfmt moves an unbreakable argument of a chain element to its own line even if it still overflows there; rustfmt leaves the element on one line.
=.A single argument that cannot break and does not fit on the line of a chain element moves to its own line, even when it overflows there too, because it then overflows by fewer columns. rustfmt leaves the element on one line when the argument does not fit on its own line either.
= moves to the next line. to keep its arguments on one line #apfmt breaks a short chain before . to keep its arguments on one line; rustfmt keeps the chain on one line and breaks the arguments.
When breaking a chain before each element lets every element fit on one line, apfmt prefers that to keeping the chain on one line with the arguments of its last element one per line, even with a single element. A receiver followed by .method(a, b, c); takes two lines where rustfmt takes five.
The Style Guide breaks a chain that does not fit before each element and prefers each element on one line. rustfmt keeps such a short chain on one line and breaks the arguments of its last element. In the head of a control-flow expression, { then goes on its own line.
match head#edition 2024apfmt removes blank lines at the start and end of every body; rustfmt keeps some in inline modules and extern blocks.
apfmt removes blank lines after { and before } of every body of items or statements. rustfmt does the same for functions, impls and traits, but keeps the blank lines after { of an inline module and at both ends of an extern block. The Style Guide does not cover them.
| Body | rustfmt after { | rustfmt before } | apfmt |
|---|---|---|---|
fn, impl, trait | removed | removed | removed |
mod m { ... } | kept | removed | removed |
extern "C" { ... } | kept | kept | removed |
apfmt puts code that continues after a comment on an indented line; rustfmt often leaves such a statement as written.
apfmt treats code that continues after a comment like any other continuation line and indents it. rustfmt usually leaves such a statement as written, so the comment and the code stay in their original columns.
=> after a trailing comment on a pattern#edition 2024rustfmt puts => at the indentation of the pattern. apfmt puts it on an indented line. The Style Guide has no rule for this.
=#edition 2024rustfmt leaves the statement as written. apfmt indents the comment and the right side like code. Both format a const the same way.
rustfmt leaves the rest of the expression at its columns in the source. apfmt puts it on an indented continuation line.
rustfmt continues the expression at the indentation of the statement. apfmt continues it on an indented line.
rustfmt moves the comment to its own line at the indentation of the keyword, with { after the head. apfmt keeps the comment after the keyword and puts the head on an indented line and { on its own line, as the Style Guide asks for any broken control line.
= of a let-else#edition 2024rustfmt leaves the statement as written. apfmt puts the initializer on an indented line and else { on its own line, as the Style Guide asks after any initializer moved to the next line.
where of an impl with an empty body is on its own line #apfmt puts where on its own line even when the impl body is empty; rustfmt keeps a short impl and its where on one line.
where over its separate request for an empty impl on one line.The Style Guide puts where on its own line. With an empty body, rustfmt keeps a short impl and its where on one line (impl<I> A for B<I> where I: C {}). On a header that fills the line, it leaves where at the end of the line and drops the trailing comma of the predicate.
The Style Guide also asks for an empty impl to be on one line, so it contradicts itself here.
impl#edition 2024= moves to the next line by the lines it saves #apfmt moves the right side of = to the next line only when that saves enough lines; rustfmt moves it for any saved line or because of how its first line ends.
When the right side of a let, an assignment or a let-else is broken on the line of =, apfmt moves it to the next line (block-indented) only when that saves at least two lines. One saved line is enough for two shapes: a chain that is on one line except for the arguments of its last element, and a call whose single argument hugs it. rustfmt moves the right side whenever the next line saves one line. In a few statements, rustfmt's result is therefore one line shorter.
rustfmt also moves the right side when its first line on the line of = ends with (, { or [ and on the next line does not, however many lines it takes there. apfmt goes only by the number of lines. rustfmt's rule can cost lines: on the next line, a chain that no longer fits on one line is broken before every element.
A broken match or if head on the line of = stays there in apfmt when moving it saves no line. rustfmt moves it because its first line ends with (.
apfmt keeps a comment that ends the line of an opening delimiter on that line; rustfmt moves it to the next line.
A comment that ends the line of (, { or [ stays there, after a space. If the line would then exceed max_width, the comment moves to the first line of the body. rustfmt moves such a comment to the next line.
A block comment followed by code on the line of the opening delimiter does not end the line. apfmt keeps it in front of that code, while rustfmt puts the comment and the code on separate lines. The same rule holds for blocks and broken lists.
apfmt keeps the relative columns of a multi-line block comment when it moves; rustfmt reindents its lines.
When a multi-line block comment moves, apfmt keeps the columns of its lines relative to its start. rustfmt reindents every line to the indentation of the code. apfmt prints a comment unchanged if one of its lines is indented less than the start of the comment or with a tab, because the relative columns cannot be kept.
apfmt may keep a short chain on one line with its last argument on its own line; rustfmt breaks before every element and hugs the argument.
When a chain whose last argument is a call does not fit, apfmt may keep the chain on one line and put the argument on its own line. rustfmt breaks before every element and hugs the argument.
fn_call_width goes on its own line #In apfmt, a breakable single argument wider than fn_call_width is never on one line in the call; rustfmt allows that if the call fits within max_width.
A single call argument that can break and is wider than fn_call_width is not kept on one line in the call, even where it would fit. apfmt hugs it if its first line (inner() is within fn_call_width, and otherwise puts it on its own line. rustfmt hugs under the same condition, but otherwise puts the whole call on one line if it fits within max_width. As a result, a longer function name can take fewer lines than a shorter one.
An argument that cannot break at all, such as a::B::new(), stays on one line within max_width in both formatters.
= #When a chain hugging its last argument does not fit on the line of =, apfmt breaks before the element; rustfmt moves the chain to the next line or breaks the arguments.
When a chain that hugs its last argument does not fit on the line of =, apfmt breaks it before the element, which takes as many lines as moving it to the next line. rustfmt moves it to the next line or breaks its arguments.
apfmt gives a closure braces so that it can hug the call; rustfmt puts the arguments one per line and keeps the closure without braces.
{}. Neither layout is clearly better.When a closure argument without braces does not fit on the line of the call, apfmt adds braces and hugs it. rustfmt puts the arguments one per line and leaves the closure without braces. Both take about the same number of lines.
=> #apfmt breaks a chain in a match arm body on the line of =>; rustfmt may put it in a block instead.
A chain that does not fit on the line of => is broken before its elements there. rustfmt also considers a block with the chain on one line, and prefers it in cases like the sample below. apfmt has no block and takes one more line. The Style Guide does not rank the two.
apfmt hugs a chain argument only if its elements stay attached to the call; rustfmt may start an element at the indentation of the statement.
rustfmt also hugs a single chain argument when the chain breaks before an element, so that element starts at the indentation of the statement, as if it applied to the statement. apfmt hugs a chain only if its elements are on the line of (, where only the arguments of the last one may break, or if a single element follows the closing delimiter of a broken root ().into()), ).await?)). The Style Guide also joins the next element to the closing line of a multi-line root. Otherwise the argument goes on its own line.
= stays there #apfmt keeps a chain broken before its elements on the line of =, even where the next line would save one line; rustfmt moves it.
When the right side of a let or an assignment is a chain whose elements each fit on a line when it is broken on the line of =, apfmt keeps it there. One level deeper, on the next line, the last element no longer fits on a line, so the chain would have to stay on one line with its last arguments broken. rustfmt compares the number of lines and moves the chain when that saves one.
? after = goes on one line on the next line #apfmt moves a single-element chain ending with ? to one line after =; rustfmt breaks it before the element.
=.A right side that is a single-element chain ending with ? (a.b(…)?) and fits on one line on the next line goes there, following the order of layouts the Style Guide gives for let. rustfmt counts the ? as a chain element, so chain_width (60 by default) limits the chain on one line, and rustfmt breaks it before the element on the line of =. Without the ?, rustfmt also moves it to the next line. Both results take two lines.
{ match ... } as a closure body keeps its braces where the head does not fit #apfmt and rustfmt differ on when the braces around a closure's match body are removed.
match line. The Style Guide prefers no braces: rustfmt follows it in the first case and apfmt in the second."Use closures without the enclosing {}, if possible" (Style Guide). match is not a control-flow expression for this rule, so both formatters remove the braces of |x| { match x { ... } }. They differ in these cases:
|x| match head { does not fit on one line, apfmt keeps the braces, and breaks the head inside them if it does not fit there either. rustfmt removes them and breaks the head. A closure without braces in the source stays without them in both.match, rustfmt keeps the braces, and apfmt removes them.{ { x } } with a long body or after broken parameters, apfmt removes the outer braces, and rustfmt keeps them.Use closures without the enclosing {}, if possible|x| match#edition 2024match#edition 2024apfmt removes the block around a match arm body even with a comment inside the expression; rustfmt keeps it.
"If the body is a single expression with no line comments and not a control flow expression, start it on the same line as the left-hand side" (Style Guide). apfmt reads "no line comments" as comments directly in the block. A block that contains only an expression loses its braces even when a comment is inside that expression, for example in a match arm, an unsafe block, a closure body or between arguments, and also from nested blocks. rustfmt keeps the block whenever there is a comment anywhere in its text.
A comment directly in the block keeps the block in both. A comment after the block (=> { f(a) } // c) keeps it in neither.
If the body is a single expression with no line comments and not a control flow expression, start it on the same line as the left-hand side
unsafe block#edition 2024match head that fits on one line only in the block goes in the block #apfmt puts a match head in the block of its arm when it fits on one line only there; rustfmt may put { on its own line.
{ on a line of its own.A match as an arm body stays on the line of => when its head and { fit there. Otherwise, if the head fits on one line inside a block, apfmt puts it in the block. rustfmt does that too when the head itself does not fit, but when only { does not, it puts { on a line of its own.
A head that breaks either way follows the braces of the source in both formatters. The Style Guide does not say whether match is a control-flow expression, which would require a block.
{#edition 2024if or match after = moves to the next line rather than break its head #apfmt moves an if or match to the next line rather than put { on its own line; rustfmt keeps it on the line of =.
When the head of an if or a match on the right side of = fits on the line of = only without {, rustfmt puts { on a line of its own. apfmt moves the expression to the next line, where the head and { fit. The Style Guide says: "If the control line is broken for any reason, then the opening brace should be on its own line".
If the control line is broken for any reason, then the opening brace should be on its own line
match whose { does not fit#edition 2024apfmt keeps a multi-line string on the line of the opening delimiter if the source has it there; rustfmt decides by the length of the name in front of it.
A single string literal spanning several lines, as the only call argument, array element, macro argument or attribute argument (including name = "..."), stays on the line of the opening delimiter if it is there in the source, and moves to a line of its own if it is not. This follows the author, as Prettier and Ruff do. Code formatted by rustfmt stays as it is, except for the trailing comma of a string left on its own line in an attribute. A tuple (r#"...",) or a string with a comment in front of it is always broken.
A hugged string moves only if that can make it fit: when its first line does not fit after the delimiter, or when its last line fits only without the closing delimiter and what follows. If its last line is at least max_width wide, moving cannot help, so the string stays hugged, as in Ruff.
rustfmt hugs such a string only after a callee shorter than tab_spaces (f(r#", [r#"), so the layout depends on the length of the name. It also ignores the width: it hugs after a short callee, in an array and for name = "..." even when that overflows, and moves the string after a longer callee even when that does not help. From a last line of 100 columns, it leaves a chain and a bare string in an attribute unformatted.
In an attribute, rustfmt hugs name = "..." whenever its first line is at most attr_fn_like_width wide, even if the source has it on its own line, and never hugs a bare string. apfmt follows the source in both cases.
The Style Guide does not cover this: a literal is not one of its combinable expressions, and a multi-line argument breaks the call.
apfmt keeps a generics clause on one line and breaks the parameters or the return type instead; rustfmt breaks the generics too.
The Style Guide says: "Prefer to put a generics clause on one line. Break other parts of an item declaration rather than line-breaking a generics clause." rustfmt breaks the generics and the parameters, and apfmt breaks only the parameters.
With no parameters to break, a return type that does not fit with { moves to the next line. rustfmt does so for a short generics clause only from 102 columns. Up to 101 columns it keeps the return type on the line and puts { on its own line, which is one column over max_width at 101. It breaks a generics clause that does not fit with the return type instead.
Prefer to put a generics clause on one line. Break other parts of an item declaration rather than line-breaking a generics clause.
apfmt keeps a tuple type on one line where it fits; rustfmt may break it anyway.
"Avoid breaking lines in types where possible" (Style Guide). rustfmt breaks a tuple type even when it would fit on its line, in a return type, a parameter, a generic argument or a field. In apfmt, a return type that does not fit moves to the next line if the function has no parameters, and breaks after broken parameters. A single tuple generic argument that must break hugs <>.
Avoid breaking lines in types where possible
for breaks before in #apfmt breaks a for head before in, as the Style Guide asks; rustfmt breaks after in.
The Style Guide breaks a for head that does not fit before in, and block-indents the in line. rustfmt breaks after in. The style team settled this rule in style-team#62 ("the in and = should go on the next line, like other binops"), but rustfmt never implemented it.
The same applies after a broken or-pattern: its last line takes the in only when the iterable fits there too.
An iterable that does not fit on the line of for is not broken there either, unless the in line cannot be on a single line: apfmt prefers breaking before in to breaking the iterable on the line of for. Neither the Style Guide nor the style team discussion ranks these two options. A chain wider than chain_width cannot be on the in line, so it breaks before every . on the line of for, as in rustfmt and as in if and while.
The Style Guide also allows a head whose last line is closing delimiters followed by {. apfmt uses this for an iterable that cannot be on a single line, such as a hugged closure, as rustfmt does. After a broken pattern and a broken iterable, { follows the closing delimiters, while rustfmt breaks after in.
apfmt moves a comment behind an operator in front of it; rustfmt breaks after the operator.
A broken operator chain starts its lines with the operator (Style Guide), so apfmt moves comments behind an operator in front of it, as if the source had them there. rustfmt keeps the source and breaks after the operator. A block comment followed by its operand on the same line stays with the operand.
apfmt puts the comment before the operator, and the operator starts the next line.
apfmt keeps the comment on its own line, and the operator starts the line after it.
; moves after it #apfmt puts ; right after the code and the comment after it; rustfmt leaves ; on its own line after the comment.
; alone on its line carries no meaning, and the comment still follows its statement.rustfmt leaves the ; on its own line after a comment. apfmt puts the ; right after the code and the comment after it.
apfmt keeps blank lines between the elements of a broken method chain; rustfmt leaves such a statement as written or removes them.
apfmt keeps a blank line between the elements of a broken method chain, even without a comment after it, because it groups the calls of a builder. rustfmt usually leaves such a statement as written, and otherwise removes the blank line. The Style Guide has no rule for this. Prettier keeps these blank lines too, but also breaks the chain because of them, while apfmt joins a chain that fits on one line and drops them.
Operator chains are different: in both formatters, their operands lose blank lines unless a comment follows, as call arguments do.
apfmt keeps a blank line before an own-line comment in every list; rustfmt removes it between call arguments and array elements.
A blank line before an own-line comment separates the comment, which belongs to the next item, from the item above. apfmt keeps it in every list. rustfmt removes it between call arguments and array elements, where it removes all blank lines, as apfmt does when no comment follows. rustfmt decided to remove blank lines inside expressions in rustfmt #1172 and implemented that in rustfmt #1822.
Lists of named fields and declarations keep all their blank lines in both formatters when broken one item per line: struct definitions, enum variants, parameters, and struct literals and patterns.
A blank line next to a comment between two attributes of the same kind also stays in apfmt, and rustfmt removes it. Without a comment both remove it, except in match: rustfmt also removes the one between an inner attribute and the #[...] of the first arm, and apfmt keeps it there, as at the start of a file.
Both fill simple call arguments on lines, but apfmt measures comments exactly, keeps a comment after ( in place and does not fill multi-line strings.
Both formatters fill simple call arguments on lines, several per line, although the Style Guide asks for one argument per line. They differ in three details:
( stays there**, as in all other lists. rustfmt moves it to the next line.short_array_element_width_threshold long.(#edition 2024apfmt does not hug a closure just to save indentation for a line that overflows anyway; rustfmt gives up on such a closure body.
A line that overflows max_width in every layout, such as a long comment or a macro call that cannot be broken, overflows by only a few more columns when indented deeper, however long it is. apfmt therefore does not hug a closure to save that line a level of indentation if the line of the call then overflows. rustfmt gives up on such a closure body: it copies it unchanged after putting the arguments one per line. apfmt formats the body, and moves the right side of a let to the next line when the call then fits there.
With overflow_line_penalty = 0, apfmt hugs the closure too.
vec! is laid out like a repeated array #apfmt gives vec![x; n] the layout of [x; n]; rustfmt breaks the vec! differently.
vec![x; n] then has the same layout as [x; n].apfmt lays out vec![x; n] like the array [x; n], matching what rustfmt does for arrays. rustfmt breaks the vec! over more lines, and without a trailing comma, as in other macro arguments.
apfmt indents the generic arguments of a broken bound one level; rustfmt indents them and the > one level deeper.
When a bound breaks inside its <>, apfmt indents the arguments one level from the bound and puts > at the indentation of the bound, like any other block-indented list. rustfmt indents the arguments and the > one level deeper, in where, in generics and after dyn.
where#edition 2024() #apfmt always removes a space before the ( of a macro call; rustfmt keeps it when it cannot parse the arguments.
rustfmt keeps a space from the source in df! ( when it cannot parse the arguments. apfmt always removes it.
async #apfmt removes the braces of a closure body in macro arguments and around async blocks with comments; rustfmt keeps them.
{}."Use closures without the enclosing {}, if possible" (Style Guide). rustfmt keeps the braces in the arguments of a macro call and around an async block with a comment inside it. apfmt removes them in both, as anywhere else. A comment directly in the braces keeps them in both formatters.
Use closures without the enclosing {}, if possibleasync with a comment inside#edition 2024In these other places, apfmt keeps comments closer to where they were written.
rustfmt leaves the statement unformatted. apfmt formats it and keeps the comments in place.
if and else#edition 2024rustfmt puts every comment on its own line. apfmt keeps them inline.
} in front of else#edition 2024rustfmt moves the comment to its own line. apfmt keeps it after }.
(#edition 2024rustfmt joins the comment with the arguments on one line. apfmt keeps it on its own line, and the list stays vertical.
where#edition 2024rustfmt joins the parameters on the line of ( or moves the comment to its own line. apfmt keeps the parameters broken and the comment after ).
rustfmt reprints the comment with its indentation from the source, aligned too far. apfmt indents it like the items.
rustfmt moves the comment to its own line. apfmt keeps it in front of the item; the Style Guide spaces an inline block comment like an identifier.
rustfmt puts the comma of the previous arm on the next line, in front of the comment. apfmt keeps the comma after the previous arm.
rustfmt writes struct F {/* c */}. apfmt writes struct F { /* c */ }, spaced like an item.
rustfmt keeps the spaces. apfmt writes [/* c */], like f(/* c */).
rustfmt puts } on the next line. apfmt keeps the body on one line.
rustfmt moves the comment to its own line. apfmt keeps it on the line of the opening delimiter.
When imports are sorted, apfmt moves every comment with its import; rustfmt leaves some behind or mixes them up.
When imports are sorted, apfmt moves every comment with its item. rustfmt leaves some comments behind or mixes them up within a list. For comments that rustfmt drops, see "Comments are never dropped".
rustfmt leaves the comment on the second line, where it now describes the wrong import. apfmt moves it with use d;.
} of a sorted list#edition 2024rustfmt moves the comment in front of other items. apfmt keeps it at the end.
{ of a sorted list#edition 2024rustfmt moves the comment in front of the item that was first in the source. apfmt keeps it on the line of {.
apfmt applies width limits exactly; rustfmt is one or two columns stricter in some places.
apfmt applies max_width and the width heuristics to the exact width of a line or a part of it. With the default settings, rustfmt is one or two columns stricter in these places:
use item with a list fits on one line only within 98 columns. apfmt keeps it on one line up to max_width (100).{ follows a return type after broken parameters only within max_width minus the indentation of the line: ) -> Ret { must end within 96 columns at an indentation of 4 and 84 at 16, as if the indentation were counted twice. apfmt keeps it there up to max_width.? fits on one line only within chain_width - 1 (59). Without the ?, and in apfmt in both cases, the limit is chain_width (60). The whole line of such a chain also fits only within max_width - 1 (99).use list, and arrays or arguments of simple items) are filled up to column 99. apfmt fills them up to max_width.), so Ok(Bar(ppp( exactly within fn_call_width is not hugged.if let pattern stays on the line of }) only within 97 columns, or 95 for a chain ending with ?. apfmt keeps it there up to max_width.use list at 98 to 101 columns#edition 2024? at chain_width#edition 2024max_width#edition 2024{ after a return type#edition 2024if let pattern#edition 2024apfmt breaks closure parameters to avoid an overflow in the body; rustfmt moves the closure to the next line and lets the line overflow.
max_width and takes one more line. Neither layout is clearly better.When the parameters of a closure after = fit on the next line but a line of the body then overflows, apfmt keeps the closure on the line of = with its parameters aligned, so the body is indented one level less. rustfmt moves it to the next line and lets the line overflow.
Both align broken closure parameters, but differ in the return type, blank lines and broken patterns.
Both formatters align closure parameters that do not fit on one line after |, although the Style Guide asks for block indentation and a trailing comma. apfmt differs in the details:
let.=, where rustfmt moves them.apfmt moves a type that fits nowhere on the line of : to the next line, also for a let; rustfmt leaves such a let unformatted.
When a type does not fit, both formatters keep it on the line of : and break inside it if they can, although the Style Guide breaks after : first. If no layout on that line fits, apfmt moves the type to the next line, block-indented, including for a let. rustfmt does that only for const and static, and leaves such a let unformatted.
apfmt breaks return type bounds before every + and block-indents them; rustfmt overflows or keeps them at the indentation of fn.
+ in types.After broken parameters, a return type with + that does not fit on the line of ) is broken before every +. rustfmt leaves it on one line, over max_width.
With no parameters to break, apfmt block-indents the + lines of a broken impl type: "Break types with + by breaking before the + and block-indenting the subsequent lines" (Style Guide). rustfmt keeps them at the indentation of fn.
Break types with+by breaking before the+and block-indenting the subsequent lines
; is broken when the ; does not fit #apfmt breaks a return type when the ; after it does not fit; rustfmt lets the line overflow.
max_width.When a function has a body and ) -> Ret fits but { does not, both put { on its own line. Without a body, rustfmt leaves ) -> Ret; on one line even when the ; makes it exceed max_width. apfmt breaks the generic arguments of the return type.
apfmt breaks a use list after a path that is too long; rustfmt leaves the line too long.
When use path::{a, b}; exceeds max_width because of a long path, rustfmt leaves the line too long. apfmt breaks the list, as the Style Guide describes: "If an import does require multiple lines ..., then break after the opening brace and before the closing brace, use a trailing comma, and block indent the names". Less of the line then overflows. The path itself is never broken.
If an import does require multiple lines
use list with a comment stays filled #apfmt fills a broken use list that has a comment; rustfmt puts its items one per line.
rustfmt puts the items of a broken use list one per line when the list has a comment. apfmt fills them on lines as in a list without one, and a trailing comment ends its line. The Style Guide does not say which is right.
apfmt sorts imports that differ only in the alias; rustfmt keeps their source order.
a is a prefix of a as b.rustfmt compares a, a as b and a as c as equal and keeps their source order. apfmt sorts them, with no alias first and then by the alias. The Style Guide version-sorts the names, and a is a prefix of a as b.
apfmt normalizes imports at any depth and keeps duplicate names; rustfmt does the opposite in both.
apfmt applies the Style Guide's normalizations at any depth. It removes an empty b::{} in a list, where rustfmt leaves use a::{ x}; for use a::{x, b::{}};, and removes the braces of a list that has become a single item.
rustfmt removes duplicate names in a list (use a::{b, b}; becomes use a::{b};). apfmt keeps them, because the Style Guide does not ask for their removal.
rustfmt can fail on such imports with the error "left behind trailing whitespace". apfmt does not.
apfmt block-indents an index that does not fit; rustfmt breaks before [.
An index that does not fit on one line is block-indented between the brackets and never broken before [, as in the Style Guide section "Array accesses, indexing, and slicing". rustfmt breaks before [.
apfmt treats a call of a chain's result as part of the chain; rustfmt indents its arguments from the start of the chain.
In a call of a chain's result (a.b()(x), a.handlers[i](x)), the arguments follow the last element on its line, as an index does, and a broken argument list is block-indented from that line. rustfmt indents it from the start of the chain.
apfmt also hugs a const block or a macro call it cannot parse as a last argument; rustfmt puts the arguments one per line.
const block is like other blocks.The last of several call arguments may hug the call when it is a const block (f(a, const {), like other blocks. rustfmt puts the arguments one per line. A macro call that is copied unchanged may also hug an array or a call as its last item (&[first, json!({), where rustfmt puts the items one per line.
const block#edition 2024& counts as another closure argument #apfmt counts a closure behind & as another closure argument; rustfmt does not.
& does not change what the argument is.The last of several call arguments overflows if it is a closure and "there are no other closure arguments" (Style Guide). apfmt looks through & and &mut, both for the last argument and for the others, so &|a| a + 1 counts as another closure. rustfmt hugs the last closure then, while it breaks the same call without the &.
there are no other closure arguments
&|a| in front of a closure#edition 2024apfmt breaks a callee only where the arguments cannot be broken; rustfmt may break inside its generic arguments.
"Prefer not to break a line in the callee expression" (Style Guide), so apfmt breaks the arguments first. rustfmt breaks inside the generic arguments of a callee path when that saves lines.
A callee that cannot be on one line keeps the arguments on its last line (})(input)) in both formatters. The Style Guide would put them one per line ("or the callee is multi-line"), which would break the common pattern of a closure called immediately.
A callee in parentheses stays together in both formatters.
Prefer not to break a line in the callee expression
as only #apfmt breaks a series of casts before the first as only; rustfmt breaks before a later as.
The Style Guide says: "if chaining a series of as casts that require line-breaking, and line-breaking before the first as suffices to make the remainder fit on the next line, don't break before any subsequent as; instead, leave the series of types all on the same line". rustfmt breaks before a later as instead.
Both formatters break a single cast before as, and both allow as T to follow a broken operand on its last line.
if chaining a series ofascasts that require line-breaking, and line-breaking before the firstassuffices to make the remainder fit on the next line, don't break before any subsequentas; instead, leave the series of types all on the same line
where after broken generics of a function is on the line of ) #apfmt writes where on the line of ) after broken generics; rustfmt puts it on its own line.
"If immediately following a closing bracket of any kind, write the keyword where on the same line" (Style Guide). apfmt writes ) where after any broken signature of a function without a return type. rustfmt does so only when the parameters are broken: after broken generics followed by (), it puts where on its own line.
After broken parameters, both write ) where.
If immediately following a closing bracket of any kind, write the keyword where on the same linewhere predicate on lifetimes breaks after : #apfmt breaks a long where predicate on lifetimes after :; rustfmt leaves the line too long.
max_width.A where predicate on lifetimes that does not fit on its line breaks after :, block-indented, like other bounds. rustfmt leaves the line too long.
where is removed #apfmt removes a where without predicates, keeping its comments; rustfmt keeps it in some places.
where without predicates has no effect, and open issues in rustfmt propose removing it there too.apfmt removes a where without predicates from every item, as rustfmt does, except that rustfmt keeps it after a return type and writes where { at column 0 when the source has where { on its own line in an indented item. Both are open issues in rustfmt (#5407, #6385).
With a comment right after or in front of where, apfmt keeps the comment and removes where, while rustfmt keeps where (fn f() where /* c */ {}). Code under #[rustfmt::skip] and macro arguments are not changed.
where#edition 2024where#edition 2024~const is kept #apfmt keeps ~const as written; rustfmt rewrites it to [const].
apfmt keeps the syntax of the source: T: ~const Clone stays, while rustfmt rewrites it to T: [const] Clone.
apfmt keeps a struct pattern field that must break anyway on the line of its name; rustfmt moves it to the next line.
A field pattern that does not fit on the line of its name moves to the next line if it fits there on one line, in both formatters. A pattern that has to break anyway stays on the line of the name in apfmt. rustfmt moves it to the next line too, so its fields are indented two levels.
else of a let-else after a single-line initializer #apfmt follows the Style Guide for else after a single-line initializer; rustfmt moves the initializer to the next line at exactly max_width.
; that a let-else does not have there."If the let statement components preceding the else can be formatted on a single line, but the else { does not fit on the same line, break the line before the else" (Style Guide). rustfmt keeps a column free for the ; of a plain let, so when the pattern and the initializer take exactly max_width, it moves the initializer to the next line.
else { follows the initializer only when its last line is just closing delimiters at the indentation of let (Style Guide). After a chain whose last element follows the closing delimiter, that line is ).method(), so else goes on its own line. rustfmt puts .method() else { on a line of its own. After a broken pattern, ) else { stays on the last line of the initializer, where rustfmt writes ) and else { on separate lines. A pattern that fits on one line is not broken to keep the initializer on the line of =.
If the let statement components preceding the else can be formatted on a single line, but the else { does not fit on the same line, break the line before the else
max_width wide#edition 2024if let guard moves after = as in if let #apfmt moves the right side of an if let guard after =, as in if let; rustfmt breaks the pattern instead.
if. Such guards are rare in practice."Prefer breaking after the = for any let sub-expression in an if or while expression that does not fit" (Style Guide). apfmt reads a match arm guard the same way: the right side moves to the next line if it fits on one line there. rustfmt does so in if let, but in a guard it breaks the pattern.
Prefer breaking after the=for anyletsub-expression in aniforwhileexpression that does not fit
apfmt puts a loop as a match arm body in a block; rustfmt keeps it on the line of =>.
loop is one.The Style Guide puts a control-flow expression (if, loop, while, for) in a block as a match arm body. rustfmt does so for all but loop, which it keeps on the line of =>.
unsafe blocks are on one line as statements #apfmt keeps only unsafe blocks on one line as statements; rustfmt also keeps async and const blocks on one line.
A single-line block is "either used in expression position (not statement position) or is an unsafe block in statement position" (Style Guide). rustfmt breaks a plain block statement too, but puts other blocks, async { 1 }; and const { 1 };, on one line.
either used in expression position (not statement position) or is an unsafe block in statement position
apfmt fills or-patterns of single tokens on lines whatever their width; rustfmt limits each alternative to 20 characters.
The Style Guide fills an or-pattern on lines when every alternative is small (a single token, & of one, or (one,)). rustfmt also requires each alternative to be at most 20 characters wide, so it puts longer names one per line, and it leaves room for => on every line of the pattern.
With a comment among the alternatives, a pattern that does not fit on one line has one alternative per line in apfmt, as a pattern with larger alternatives does. rustfmt leaves such a pattern as written, including its spacing. The Style Guide does not mention comments here.
todo! and unimplemented! are format-string macros #apfmt treats todo! and unimplemented! as format-string macros; rustfmt does not.
The Style Guide has a layout for macros that take a format string: the arguments in front of the format string on one line, the format string on its own line, and the arguments after it on one line, when they are all simple. rustfmt has a fixed list of such macros that does not include todo! and unimplemented!, so it puts their arguments one per line.
{ after an unsafe block ending a broken head #apfmt puts { on its own line after an unsafe block ending a broken head; rustfmt writes } {.
When a broken control line ends with an unsafe block, apfmt puts { on its own line, as after any broken control line. rustfmt puts it after the closing }, as } {. apfmt keeps a block on its operator's line only for a plain block as the last operand.
unsafe operand#edition 2024apfmt leaves an empty file empty; rustfmt writes a single line break.
A file with no code or comments (empty, or only whitespace) is formatted to an empty file. rustfmt writes a single line break. Any other output ends with exactly one line break in both formatters.
newline_style = "Auto" keeps the line endings of the file #With newline_style = "Auto", apfmt keeps the line endings of the file; rustfmt converts \r\n to \n when it changes anything.
rustfmt documents Auto (the default) as "Use the line ending style of the first line ending found in the file". In practice, it always writes \n when formatting changes anything in the file, so a file with \r\n line endings is converted unless it is already formatted. apfmt does what the option says and writes the line endings of the first line of the source, so such a file keeps \r\n on every line. Unix, Windows and Native behave as in rustfmt.
\r\n line endings that needs formatting#edition 2024\r\n line endings that is already formatted#edition 2024rustfmt leaves such a file as it is, so both keep \r\n.
No differences match.