Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
use path::to::module::*;
use path::to::EnumName::*;

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).
mod alpha {
    pub struct Config;
    pub(crate) fn internal_init() {} // Restricted visibility: imported if in allowed scope
    fn private_check() {}            // Private: ignored by glob import
    
    pub enum Status {
        Active,
        Inactive,
    }
}

mod beta {
    pub fn internal_init() {}
}

// Glob import brings `Config`, `internal_init`, and `Status` into scope
use alpha::*;

// Glob import brings `Active` and `Inactive` variants into scope
use alpha::Status::*;

// Explicit import shadows `alpha::internal_init`
use beta::internal_init;

fn main() {
    let _cfg = Config;         // Resolves to alpha::Config
    let _state = Active;       // Resolves to alpha::Status::Active
    internal_init();           // Resolves to beta::internal_init due to explicit import precedence
}
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.
mod internal_api {
    pub struct Request;
    pub struct Response;
}

// Re-exports all accessible items from `internal_api` to the current module's public interface
pub use internal_api::*; 
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.
Master Rust with Deep Grasping Methodology!Learn More