Auto combining shared prefix "use ...."

  1. Suppose we have a code block like:
use foobar::A;
use foobar::B;
use foobar::C;

Is there some way of having rust-fmt (or some other tool) to automatically replace it with:

use foobar::{A, B, C}

?

  1. I find the latter easier to read / more organized; but often end up with the former due to IntelliIJ auto adding imports locally which I then move to lib.rs

You can try creating rustfmt.toml with the following contents

merge_imports = true

See https://rust-lang.github.io/rustfmt/#merge_imports

(This requires using nightly toolchain though, as the option is still unstable)

3 Likes

Works perfectly (even from IntelliJ). Thanks!