Hiding in Use & Re-Export

Haskell has a useful feature for hiding specific items in a glob import, allowing you to import everything except the items named as "hiding." The same could be useful in Rust, both for local imports and for re-exports.

I'd imagine a syntax like:

use crate::some_module::{* hiding SomeType};

Of course bikeshedding is welcome.

I did a cursory look on the forum and existing RFCs and didn't see prior discussion. Has this idea come up before? If it has I'd love to read the state of discussion.


What is the benefit of this? Normally glob imports are a lower priority that specifics so if there is a name collision it would just be with another glob import and then it seems backwards to restrict that named import in all the other places. Instead of, for instance, specifically importing the one you care about.

1 Like

Typical advice for Rust is that glob imports should only be used for "small" modules, typically prelude modules explicitly intended for use with glob imports.

A well designed prelude won't contain any names likely to be problematic when brought into scope, so there wouldn't be a strong rationale for hiding from a prelude.

For the public re-export case, it's almost certainly better to just spell out all the types you want to re-export explicitly rather than use a glob, or just export the module.


I understand the thought behind this, but for me personally this makes it harder for me to understand what's going on. I used to do glob imports, but have started avoiding them because it makes it less clear to me where something came from (not to mention it makes it harder for clippy to warn me about unused imports). This feels even messier. I know I can avoid it my own code, but if I'm reading someone else's code, then this will make understanding where things came from in their code that much harder.


Thanks for all the feedback, and generally I agree. The context here was in generated code where the hiding ability may make the generation easier, but it's not terribly consequential. If you put a feature in people will use it, and I agree that this mechanism is an anti-pattern.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.