Rust Lesson 30 – Modules | Dataplexa

Modules in Rust

In this lesson, you will learn about modules in Rust. Modules are used to organize code, control visibility, and manage large Rust projects in a clean and maintainable way.

As Rust applications grow, modules become essential for structuring code into logical units.


What Is a Module?

A module is a namespace that groups related functions, structs, enums, and constants together.

Modules help you:

  • Organize large codebases
  • Avoid name conflicts
  • Control what code is public or private

Creating a Module

Modules are created using the mod keyword.

mod math {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

By default, everything inside a module is private.


Public vs Private Items

To make functions or types accessible outside a module, you must use the pub keyword.

mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

Only public items can be accessed from outside the module.


Using a Module

You can access items inside a module using the scope resolution operator ::.

fn main() {
    let result = math::add(2, 3);
    println!("{}", result);
}

Nested Modules

Modules can be nested inside other modules to create a hierarchy.

mod outer {
    pub mod inner {
        pub fn greet() {
            println!("Hello from inner module");
        }
    }
}

Accessing nested modules:

outer::inner::greet();

The use Keyword

The use keyword brings module paths into scope, making code shorter and cleaner.

use outer::inner;

inner::greet();

This avoids repeating long paths throughout your code.


Splitting Modules into Files

In real projects, modules are often split across multiple files.

Example file structure:

src/
 ├── main.rs
 ├── math.rs

Inside main.rs:

mod math;

fn main() {
    println!("{}", math::add(3, 4));
}

Inside math.rs:

pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

Modules and Visibility Rules

Rust follows strict visibility rules:

  • Items are private by default
  • Parent modules cannot access private child items
  • Child modules can access parent items

These rules help enforce safe and intentional APIs.


Why Modules Matter

Modules are critical for:

  • Large-scale applications
  • Reusable libraries
  • Clear project architecture
  • Maintaining clean and readable code

📝 Practice Exercises


Exercise 1

Create a module with a public function that prints a message.

Exercise 2

Create a nested module and call a function inside it.

Exercise 3

Use the use keyword to shorten a module path.

Exercise 4

Split a module into a separate file.


✅ Practice Answers


Answer 1

mod hello {
    pub fn say_hi() {
        println!("Hi!");
    }
}

Answer 2

mod a {
    pub mod b {
        pub fn run() {
            println!("Running");
        }
    }
}

a::b::run();

Answer 3

use a::b;

b::run();

Answer 4

// main.rs
mod utils;

// utils.rs
pub fn helper() {}

What’s Next?

In the next lesson, you will learn about Packages and Crates — how Rust organizes projects at a higher level using Cargo.