rust-lang / rust-lang/rustfmt

Sort items in a file to make it readable top to bottom

Open
#2,359 3 comments 16 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I love that in Rust I can declare items in a file wherever I want.

On the other hand, I also like to read source files, typically top to bottom.

It would be great if rustfmt could have an opt-in option (e.g. via cargo fmt) that would sort the items in a file, so that ideally an item is declared / implemented before it is used in the implementation of another item (cargo fmt --sort-items).

A perfect output might not be always possible, but something "good enough" would already make many of my files much more "readable" top to bottom.

Also, if this is desired (I'd like something like it), it should probably be planned with configuration in mind, for example, in some cases, a user might want to interleave tests with items (e.g. the test of an item follow or preceed the item in the source file), while in other cases the user might want to put all the tests at the bottom of the source file, sorted top-to-bottom in the same order as the items. Relating a test to an item would need some heuristic, like the test should contain the item in its name or something.

Anyways, I just ended up writing a "huge" file (>2kLOC) and had to skim a lot through some "huge" files in the std library, and would have appreciated something like this.

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 examining rustfmt's existing item-formatting behavior and the cargo fmt entry point. Define how items should be ordered, how dependencies and tests should be related, and which configuration options are needed; done means an opt-in sort-items behavior with predictable output.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.