Skip to content

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

Open
@gnzlbg

Description

@gnzlbg

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions