Rustfmt option to put all )})'s on the same line?

  1. I realize this sounds disgusting, but want to try it anyway.

  2. Is there a way for rust fmt to force all )'s }'s to the same line? (s-expr style), i.e.

// old
if a {
  if b {
    some stuff
  }
} 

// new
if a {
  if b {
    some stuff }}

this is because often times 1/3 to 1/2 of my screen real estate tend to be wasted on lines that consist of some combination of )};

I am curious if there is a way to recover this real estate (blocks is still easy to see via indentation).

No, this is not one of the currently-implemented behaviors for the control_brace_style option.

1 Like

Personally I think it's a good idea if we all use fmt with the default settings. Especially if publishing work for public consumption.

That way we all get used to how Rust looks and reading other peoples code becomes easier as we are not constantly tripped up by weird formatting. Go with the flow, as it were.

If one were joining project team, corporate or open source, one would work to whatever formatting the project guidelines lay down. Well, just consider every line of Rust code in every project ever written to be part of the same global team. Go with the flow. Make life easier for everyone.

I know everyone has strong opinions about this and preferences for this style or that. I do too. But really, there is no need to take a stand on it and stick out like a sore thumb.

As for "screen real estate". I don't know. I already save a lot by putting "{" on the same line as the "if"'s and "loop"'s etc. Where as in C I have always preferred them on a new line, by way of balance or symmetry with the closing "}".

Also running out of screen when reading a function is often a very good sign that the function is getting too long and complicated. A hint to factor things out a bit.

1 Like

@ZiCog : I understand where your argument is coming from, and your view is definitely the view of reason here. Sometimes though I look at An Implementation of J and wonder what Rust, written in such a dense style would be like. :slight_smile:

This post was flagged by the community and is temporarily hidden.

While you can definitely write very obfuscated and compressed code in Rust, I would argue it's illogical to do so.

There's no point in reducing the amount of information given to the reader; best be verbose than barren of descriptors. At least that's my perspective on the issue.

Dangerous code. Do not use. Definitely breaks code in some cases. MIT license.

Here is a prototype I have so far (patch-ing vs 1.4.36 of rustfmt)

Main goal is to make Rust code look a bit more like idiomatic Clojure, i.e. GitHub - bbatsov/clojure-style-guide: A community coding style guide for the Clojure programming language where we avoid lines that consists solely or ")" or "}"

diff --git a/src/expr.rs b/src/expr.rs
index 33add30c..14117e4b 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1640,6 +1640,7 @@ fn rewrite_struct_lit<'a>(
 
     let fields_str =
         wrap_struct_field(context, &attrs, &fields_str, shape, v_shape, one_line_width)?;
+    let fields_str = fields_str.trim_end(); // ugly hack; so sorry
     Some(format!("{} {{{}}}", path_str, fields_str))
 
     // FIXME if context.config.indent_style() == Visual, but we run out
diff --git a/src/items.rs b/src/items.rs
index 10654a2a..73a0c2b5 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -520,7 +520,6 @@ impl<'a> FmtVisitor<'a> {
 
         let list = write_list(&items, &fmt)?;
         result.push_str(&list);
-        result.push_str(&original_offset.to_string_with_newline(self.config));
         result.push('}');
         Some(result)
     }
@@ -788,13 +787,12 @@ pub(crate) fn format_impl(
             visitor.format_missing(item.span.hi() - BytePos(1));
 
             let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
-            let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
 
             result.push_str(&inner_indent_str);
             result.push_str(visitor.buffer.trim());
-            result.push_str(&outer_indent_str);
         } else if need_newline || !context.config.empty_item_single_line() {
             result.push_str(&sep);
+
         }
 
         result.push('}');
@@ -1161,7 +1159,6 @@ pub(crate) fn format_trait(
 
             result.push_str(&inner_indent_str);
             result.push_str(visitor.buffer.trim());
-            result.push_str(&outer_indent_str);
         } else if result.contains('\n') {
             result.push_str(&outer_indent_str);
         }
@@ -1354,13 +1351,13 @@ pub(crate) fn format_struct_struct(
         Some(format!("{} {} }}", result, items_str))
     } else {
         Some(format!(
-            "{}\n{}{}\n{}}}",
+            "{}\n{}{}}}",
             result,
             offset
                 .block_indent(context.config)
                 .to_string(context.config),
             items_str,
-            offset.to_string(context.config)
+            // offset.to_string(context.config)
         ))
     }
 }
diff --git a/src/matches.rs b/src/matches.rs
index a43aed09..1fcab024 100644
--- a/src/matches.rs
+++ b/src/matches.rs
@@ -132,13 +132,12 @@ pub(crate) fn rewrite_match(
     } else {
         let span_after_cond = mk_sp(cond.span.hi(), span.hi());
         Some(format!(
-            "match {}{}{{\n{}{}{}\n{}}}",
+            "match {}{}{{\n{}{}{}}}",
             cond_str,
             block_sep,
             inner_attrs_str,
             nested_indent_str,
             rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
-            shape.indent.to_string(context.config),
         ))
     }
 }
diff --git a/src/visitor.rs b/src/visitor.rs
index 34e8536b..16d1c005 100644
--- a/src/visitor.rs
+++ b/src/visitor.rs
@@ -369,7 +369,6 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
             self.block_indent = self.block_indent.block_indent(self.config);
         }
         self.block_indent = self.block_indent.block_unindent(self.config);
-        self.push_str(&self.block_indent.to_string_with_newline(config));
         self.push_str("}");
     }
 

1 Like