rust-lang / rust-lang/rustfmt

Alignment of let bindings

Open
#3,821 3 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request community driven P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Hi!
I really like what rustfmt is doing, however, one thing hurts me strongly here. It allows the vertical alignment of struct fields, but does not allow for such alignment of let bindings. Consider the following code:

impl Scene {
    fn new() -> Result<Scene, Error> {
        let doc = document()?;
        let canvas = doc.get_element_by_id("canvas").ok_or(DOMError::missing("canvas"))?;
        let data = Rc::new(SceneData { canvas, context });
        let data_weak = Rc::downgrade(&data);
        ....
    }
}

I believe it is much nicer to read the following code:

impl Scene {
    fn new() -> Result<Scene, Error> {
        let doc       = document()?;
        let canvas    = doc.get_element_by_id("canvas").ok_or(DOMError::missing("canvas"))?;
        let data      = Rc::new(SceneData { canvas, context });
        let data_weak = Rc::downgrade(&data);
        ....
    }
}

Would it be possible to add some treshold option to rustfmt for such a style? :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing rustfmt’s existing vertical alignment behavior for struct fields and how formatting options are defined. Determine the scope and semantics of an option for aligning let bindings, including the proposed threshold. Done means the option’s behavior is specified and implemented consistently for the example shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.