> ## 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 Nested Module

A nested module in Rust is a module defined within the lexical scope of another module, establishing a hierarchical namespace tree for code organization and encapsulation. Nested modules create strict privacy boundaries, requiring explicit visibility modifiers to expose internal items to parent or external scopes.

## Inline Declaration

Nested modules can be defined inline within a single file using nested `mod` blocks. By default, a nested module and its contents are private to its immediate parent module.

```rust theme={"dark"}
mod parent_module {
    // The nested module is private to `parent_module` by default
    pub mod child_module {
        // The function must also be marked `pub` to be accessible
        pub fn nested_function() {
            println!("Accessed nested function");
        }
    }
}

fn main() {
    // Path resolution uses the `::` operator
    parent_module::child_module::nested_function();
}
```

## Visibility and Privacy Rules

Rust enforces strict privacy rules across module boundaries. A parent module cannot access private items within its nested child modules, but a nested child module can access all items (public or private) in its parent module.

To control access, Rust provides granular visibility modifiers:

* `pub`: Makes the item visible to any scope that can access the module.
* `pub(super)`: Restricts visibility to the immediate parent module.
* `pub(crate)`: Restricts visibility to the current crate.
* `pub(in path)`: Restricts visibility to a specific ancestor module defined by the provided path (e.g., `pub(in crate::outer::inner)`).

```rust theme={"dark"}
mod outer {
    fn private_outer_fn() {}

    pub mod inner {
        pub(super) fn restricted_fn() {
            // Child can access parent's private items implicitly
            super::private_outer_fn(); 
        }
        
        pub mod deep_inner {
            // Visible only within the `outer` module
            pub(in crate::outer) fn deeply_restricted_fn() {}
        }
    }

    pub fn access_inner() {
        // Parent can access child's `pub(super)` items
        inner::restricted_fn();
        // Parent can access descendant's `pub(in crate::outer)` items
        inner::deep_inner::deeply_restricted_fn();
    }
}
```

## File System Mapping

When codebases grow, nested modules are typically extracted into separate files. Rust maps the module hierarchy to the directory structure. To nest a module via the file system, the parent module must declare the child module using `mod child_module;` without a block.

Given the following module tree: `crate::parent::child`

**Directory Structure:**

```text theme={"dark"}
src/
├── lib.rs
├── parent.rs
└── parent/
    └── child.rs
```

**File Contents:**

```rust theme={"dark"}
// src/lib.rs (Crate Root)
pub mod parent; // Declares the parent module

// src/parent.rs
pub mod child; // Declares the nested child module

// src/parent/child.rs
pub fn nested_function() {} // Actual implementation
```

*Note: In older Rust editions (pre-2018), `src/parent.rs` would instead be named `src/parent/mod.rs`.*

## Path Resolution

Navigating nested module trees requires specific path keywords:

* **Absolute paths** begin from the crate root using the `crate` keyword.
* **Relative paths** begin from the current module using `self`, `super`, or the identifier of a sibling module.

```rust theme={"dark"}
mod level_one {
    pub fn root_level_fn() {}

    pub mod level_two {
        pub mod level_three {
            pub fn deep_fn() {
                // Relative path stepping up two levels
                super::super::root_level_fn();
                
                // Absolute path from the crate root
                crate::level_one::root_level_fn();
            }
        }
    }
}
```

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