Defining a prelude module for a library crate

When I'm defining a new library crate, what would lead me to a define a prelude module for it?
Is it that I identify a subset of items that most users of the library will want to include into their namespace with use mylibrary::prelude::*?
Is it fair to say that for most library crates everything they define is accessed from a single module?

In my opinion, preludes are most useful when library users need to import several traits.

I prefer to explicitly name most items that I import (e.g. types, functions, macros) so when I see an identifier in code, I can easily glance at the imports to see where it comes from. Wildcard imports like use foo::*; prevent me from doing this.

However, traits are different because their names usually don't appear in the code at all, so it's not obvious which imported trait is behind any given method call. Using a prelude in this case saves me from having to keep track of the mapping from method name to trait name, and has little downside.

Some examples of preludes that mostly contain traits:

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.