Skip to main content
A glob import in Rust utilizes the wildcard operator (*) within a use declaration to bring all accessible items defined in a specified module, crate, or enum into the current namespace. This mechanism binds multiple identifiers to the local scope simultaneously without requiring explicit, itemized declarations.

Mechanics and Behavior

Visibility Rules A glob import strictly respects Rust’s privacy boundaries. It brings items into scope based on their visibility modifiers relative to the importing module. This includes items marked with pub, as well as items with restricted visibility modifiers (such as pub(crate), pub(super), or pub(in path)), provided the importing module resides within the permitted visibility scope. Private items within the target module remain inaccessible and are ignored by the glob operator. Enum Variants When applied to an enum type, a glob import brings all of the enumeration’s variants into the current scope. Because enum variants inherit the visibility of the enum itself, a glob import on any accessible enum will successfully bind all of its variants locally, allowing them to be referenced without their type namespace. Shadowing and Precedence Rust’s module system resolves name collisions between glob imports and explicit imports through shadowing rather than immediate compilation errors.
  • Explicit over Glob: An explicitly imported item or a locally defined item will always shadow an item of the same name brought in via a glob import.
  • Glob vs. Glob: If two separate glob imports bring in items with identical names, the compiler will not error at the use statements. However, if the ambiguous identifier is actually referenced in the code, the compiler will throw an ambiguity error (E0659).
Re-exporting via Glob The glob operator can be combined with visibility modifiers to re-export an entire module’s or enum’s accessible API. This flattens the module hierarchy for external consumers of a crate.
Trait Resolution When a glob import brings traits into scope, it makes the trait methods available for any types that implement those traits within the current scope. The traits themselves do not need to be explicitly named in the file to utilize their implemented methods, provided the glob import is active.
Tired of Poor Rust Skills? Fix That With Deep Grasping!Learn More