Is there a style guide for the order of use statements?

AFAIK the order of the use statements does not matter, but I would like to have them in a consistent order. Is there a style guide on how to order them?

For example:

  1. pub use statements
  2. crate imports
  3. std imports
  4. external crate imports

I have

group_imports = "StdExternalCrate"
imports_granularity = "Module"

But beware, that those require the nightly toolchain:

$ cargo +nightly fmt

That being said, I don't think that there's a general guideline on which style to use.
I'd just recommend being consistent within a project, and, maybe, even across projects for the sake of your own sanity. :wink:

There are a few rules in the Rust Style Guide, but seeing what the formatter does is probably more explicit and more complete. The order does matter in some cases, I think; for example if there's a risk of collision between what you define locally and something you may import with a * (which you shouldn't normally use), which is why they advise to put self and super first. There might be other cases.

Iirc the Rust compiler does not allow for any name clashes.

There must be another reason why they suggest that, then. Or maybe it's only an old habit from C, where it's advised to put local headers first to make sure they're self-contained?

To be honest, I find it more clear if the local imports follow the std and external imports.

I use group_imports = "StdExternalCrate" too, but I have imports_granularity = "Item". the reason is I don't like group imports of multiple items in a single line.

for example, when reviewing changes in diff view, I would prefer this:

 use crate_foo::mod_bar::item_abc;
-use crate_foo::mod_bar::item_fgh;
+use crate_foo::mod_bar::item_uvw;
 use crate_foo::mod_bar::item_xyz;

rather than this:

-use crate_foo::mod_bar::{item_abc, item_fgh, item_xyz};
+use crate_foo::mod_bar::{item_abc, item_uvw, item_xyz};

p.s., I would like a config of rustfmt to force a group of multiple items to split into separate lines, even if they are short, and even if there are only a few items , but unfortunately, there's no such options.

for example:

// I want this:
use a::b::{
   self,
   x,
   y,
   z,
};
// but `rustfmt` produces this:
use a::b::{self, x, y, z};

p.p.s.

it came to me that Linus recently criticized rustfmt of smashing grouped imports into a single line in a comment on a drm pull request:

1 Like

This is a compelling argument. However, I'd also not like my imports to bloat my file, which is why I find a new line for every item excessive.

For crate imports, is there some style guide on when to use crate:: paths vs super::?

I don't know of any official style for that. I'd say it mostly depends on your preference for absolute or relative paths.

For unit tests or modules that in the above level, I'm using super because it makes more sense.

When it's in another crate, there's no choice but to use crate (except items that are re-exported). And when it's in the same crate but too many levels above or in another path, I find crate is usually more clear.

But GitHub highlights the exact word that was changed within the line as well.

Not default Git, maybe? I wonder whether there is an option for it.

There are alternatives to Git that do it apparently, but not Git.

1 Like