> ## 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.

# Rust Module

A Rust module is a namespace that encapsulates code items—such as functions, structs, traits, `impl` blocks, and other modules—to establish scope boundaries and control visibility. Modules form a hierarchical tree structure originating from a crate root (typically `src/lib.rs` or `src/main.rs`), allowing developers to partition code logically and manage namespace collisions.

## Inline Module Declaration

Modules are defined using the `mod` keyword. An inline module contains its implementation directly within a block in the same file.

```rust theme={"dark"}
mod network {
    // Private by default
    fn connect() {}

    // Public submodule
    pub mod tcp {
        // Public function
        pub fn send() {}
    }
}
```

## File System Mapping

When a module is declared without a code block, the Rust compiler (`rustc`) defers the module's definition to an external file. The compiler resolves the module tree by looking for files that match the module's name.

```rust theme={"dark"}
// Declares the module and instructs the compiler to look for its contents
mod parser; 
```

For the declaration `mod parser;`, Rust will look for the implementation in one of two locations relative to the declaring file:

1. `parser.rs`
2. `parser/mod.rs`

To extract submodules of `parser` into their own external files, a directory structure is required. If `parser.rs` contains the declaration `mod ast;`, the compiler will look for `parser/ast.rs` or `parser/ast/mod.rs`. However, inline submodules (e.g., `mod ast { ... }`) can be defined directly inside `parser.rs` without needing a new directory.

## Visibility and Privacy Boundaries

By default, all items within a module (functions, structs, fields, methods, and submodules) are private. They are only visible to the module itself and its descendant submodules. The `pub` keyword modifies this visibility.

Rust provides granular visibility modifiers to restrict access to specific scopes:

```rust theme={"dark"}
mod system {
    pub mod security {
        // Private: Accessible only within `security` and its submodules
        fn generate_seed() {}

        // Public: Accessible from anywhere the `security` module is accessible
        pub fn encrypt() {}

        // Crate-level: Accessible anywhere within the current crate
        pub(crate) fn verify_checksum() {}

        // Parent-level: Accessible only to the parent module (`system`)
        pub(super) fn internal_audit() {}

        // Path-level: Accessible only within the specified ancestor path
        pub(in crate::system) fn share_key() {}
    }
}
```

* **`pub(in path)` Semantic Rule:** The path specified in a `pub(in path)` visibility modifier *must* be an ancestor module of the module where the item is defined.
* **Struct Field Visibility:** Making a struct public (`pub struct`) does not automatically make its fields public. Fields must be individually marked with `pub`.

## Path Resolution and Scope Import

To reference items across the module tree, Rust uses paths separated by the double colon `::`. Paths can be absolute or relative.

* **`crate::`** initiates an absolute path from the crate root.
* **`super::`** initiates a relative path from the parent module.
* **`self::`** initiates a relative path from the current module.

The `use` keyword binds a full path to a local name, bringing the item into the current module's scope to reduce verbosity.

```rust theme={"dark"}
mod server {
    pub mod router {
        pub fn dispatch() {}
    }
}

mod client {
    // Absolute path import
    use crate::server::router;
    
    // Relative path import using `super`
    use super::server::router::dispatch;

    fn handle_request() {
        // Accessed via the imported module path
        router::dispatch();
        
        // Accessed via the directly imported function
        dispatch();
    }
}
```

## Re-exporting (`pub use`)

Modules can re-export items from other modules using `pub use`. This flattens the public API, allowing external consumers to access deeply nested items as if they were defined in the re-exporting module.

```rust theme={"dark"}
mod internal {
    pub mod deep {
        pub struct Configuration;
    }
}

// Re-exports `Configuration` at the root of this module
pub use internal::deep::Configuration;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
