codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

19 / 58 topics
19Modules20Crates
Tutorials/Rust/Modules
🦀Rust

Modules

Updated 2026-04-20
3 min read

Modules in Rust

Rust's module system is a powerful feature that helps organize code into logical groups, making it easier to manage and reuse. In this section, we will explore the fundamentals of modules, how to define them, use them, and best practices for structuring your Rust projects.

What are Modules?

Modules in Rust allow you to encapsulate functionality within a single unit, which can then be reused across different parts of your program. They help in managing namespaces, reducing code duplication, and improving maintainability. Each module can contain functions, structs, enums, traits, and other modules.

Defining Modules

You can define a module using the mod keyword followed by the module name. Here’s how you can create a simple module:

// src/lib.rs or src/main.rs
mod my_module {
    fn private_function() {
        println!("This is a private function.");
    }

    pub fn public_function() {
        println!("This is a public function.");
    }
}

In this example, private_function is not accessible outside of the module, whereas public_function can be accessed using the pub keyword.

Using Modules

To use a module, you need to bring it into scope. You can do this using the use keyword:

// src/main.rs
mod my_module;

fn main() {
    // Accessing public function from the module
    my_module::public_function();
}

Submodules

Modules can contain other modules, allowing for hierarchical structuring of your code. Here’s an example with submodules:

// src/lib.rs or src/main.rs
mod outer_mod {
    pub mod inner_mod {
        pub fn public_function() {
            println!("This is a public function in the inner module.");
        }
    }
}

To use the public_function from the inner_mod, you would do:

// src/main.rs
mod outer_mod;

fn main() {
    // Accessing nested public function
    outer_mod::inner_mod::public_function();
}

File System Structure

Rust also allows you to define modules based on your file system structure. If you have a module defined in src/lib.rs, you can create a separate file for it:

// src/lib.rs
mod my_module;

And then create the corresponding file:

// src/my_module.rs
fn private_function() {
    println!("This is a private function.");
}

pub fn public_function() {
    println!("This is a public function.");
}

For submodules, you can use nested directories:

// src/lib.rs
mod outer_mod;

Create the directory and file structure:

// src/outer_mod/mod.rs
pub mod inner_mod;
// src/outer_mod/inner_mod.rs
pub fn public_function() {
    println!("This is a public function in the inner module.");
}

Paths

In Rust, you can refer to items using paths. A path is a sequence of identifiers separated by double colons (::). There are two types of paths:

  1. Absolute Path: Starts from the crate root (either crate or an external crate name).
  2. Relative Path: Starts from the current module and uses self, super, or an identifier in the current scope.
// src/lib.rs
mod outer_mod {
    pub mod inner_mod {
        pub fn public_function() {
            println!("This is a public function.");
        }
    }
}

fn main() {
    // Absolute path
    crate::outer_mod::inner_mod::public_function();

    // Relative path
    outer_mod::inner_mod::public_function();
}

Re-exporting

Modules can re-export items from other modules using the pub use keyword. This is useful for creating a cleaner API:

// src/lib.rs
mod outer_mod {
    pub mod inner_mod {
        pub fn public_function() {
            println!("This is a public function.");
        }
    }

    // Re-exporting the function
    pub use self::inner_mod::public_function;
}

Now, you can access public_function directly from outer_mod:

// src/main.rs
mod outer_mod;

fn main() {
    outer_mod::public_function();
}

Best Practices

  1. Keep Modules Small and Focused: Each module should have a single responsibility.
  2. Use Clear Naming Conventions: Choose descriptive names for modules to make your code more readable.
  3. Minimize Public API Surface: Only expose what is necessary to avoid breaking changes in the future.
  4. Organize Code by Functionality: Group related functions, structs, and enums into appropriate modules.
  5. Use Submodules for Logical Grouping: Break down large modules into smaller submodules if they become unwieldy.

Conclusion

Rust's module system is a powerful tool for organizing and structuring your code. By understanding how to define, use, and manage modules, you can create maintainable, scalable, and reusable Rust applications. Remember to keep your modules focused, minimize the public API surface, and organize your code logically to take full advantage of Rust’s modularity features.

This guide should provide a solid foundation for working with modules in Rust. As you gain more experience, you'll find that the module system is an essential part of writing idiomatic Rust code.


PreviousEnumsNext Crates

Recommended Gear

EnumsCrates