Custom prelude imports

There are many times where it would be useful to have crate-wide prelude imports. Rust already has prelude_import for std::prelude, so why not let users use it also?

#[prelude_import]
use bar::baz;

mod foo {
    use baz::Foo;
}

The motivation for this is pretty clear, but there are a couple specific use cases that I want to highlight:

  • "Add" to std::prelude - there have been long arguments about adding items like std::mem or std::mem::{replace, swap} to std's prelude, and allowing custom preludes would let users choose whatever they want.
  • Extension traits. Some libraries, such as futures for example, have extension traits that are indispensable (StreamExt, FuturesExt). These crates already have a prelude module to address this, but many crates must copy paste the import throughout their code and custom preludes would make this more ergonomic.
  • use crate::prelude::* is already a common practice, so letting users propagate it throughout their crate makes a lot of sense.
  • Make the std::prelude import less "magical".
  • Domain specific applications will use specific items from external crates over and over again throughout their entire crate. Prelude imports make this more ergonomic.

Potential downsides:

  • Some people are already against glob prelude imports, and this feature makes them a first class citizen. However, prelude imports are sometimes need, and I think this feature could be very beneficial.
  • Makes imports harder to follow for someone reading the code.
  • Prelude shadowing might cause issues?

Previous work: RFC 80

1 Like