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.

Re-exporting in Rust is the mechanism of bringing an item into a module’s scope and simultaneously exposing it as part of that module’s public interface using the pub use keyword combination. This operation binds an item from a deeper or adjacent module path to a new path, effectively decoupling the internal module hierarchy from the external public API.

Syntax and Mechanics

The fundamental syntax combines a visibility modifier (pub) with an import statement (use).
pub use path::to::item;
When the compiler encounters pub use, it performs two actions:
  1. Resolves the path to the target item and brings it into the current module’s namespace.
  2. Applies the specified visibility modifier to the imported name, allowing external code to access the item via the current module.

Variations of Re-exporting

1. Direct Re-export Exposes an item exactly as it is named in the source module.
mod internal {
    pub struct Engine;
}

// Re-exports `Engine` at the current module level
pub use internal::Engine;
2. Aliased Re-export Renames the item in the new namespace using the as keyword. This changes the identifier external consumers must use to reference the item.
mod network {
    pub struct Connection;
}

// Re-exports `Connection` as `NetConn`
pub use network::Connection as NetConn;
3. Glob Re-export Binds all public items from a target module into the current module’s namespace.
mod utilities {
    pub fn parse() {}
    pub fn format() {}
}

// Re-exports both `parse` and `format`
pub use utilities::*;
4. Restricted Visibility Re-export Re-exports can be scoped using Rust’s granular visibility modifiers (e.g., pub(crate), pub(super), pub(in path)). This restricts the re-exported item’s availability to a specific portion of the module tree.
mod core_logic {
    pub struct Processor;
}

// Re-exports `Processor` only to the rest of the current crate
pub(crate) use core_logic::Processor;

Path Resolution Behavior

For a re-export to be valid, the item being re-exported must be visible to the module performing the re-export. If Module A attempts to pub use an item from Module B, the item in Module B must be marked pub (or have sufficient visibility) relative to Module A.
mod backend {
    // Must be public for the parent to re-export it
    pub mod storage {
        pub struct Database;
    }
}

// Valid: `backend::storage::Database` is visible here
pub use backend::storage::Database;
Once re-exported, the item is subject to standard path resolution rules as if it were natively defined in the re-exporting module. External crates or modules interacting with the re-exported path do not need visibility into the original definition path.
Master Rust with Deep Grasping Methodology!Learn More