Rust
December 2, 2026 5 min read

Modules and Packages in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What Are Modules in Rust?

A module is a way to organize related functions, structs, enums, and other code together.

For example, in a student management application, we might have:

  1. Student-related functions
  2. Teacher-related functions
  3. Fee-related functions
  4. Attendance-related functions

Instead of writing everything in one file, we can organize the code into separate modules.

Example:

mod student {
pub fn show_name() {
println!("Student: Hemant");
}
}

fn main() {
student::show_name();
}

Output:

Student: Hemant

Here:

  1. mod student creates a module named student.
  2. pub fn show_name() defines a public function.
  3. student::show_name() calls the function using its module path.

2. Understanding the mod Keyword

The mod keyword is used to declare a module.

Syntax:

mod module_name {
// Code inside the module
}

Example:

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

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

fn main() {
let result1 = calculator::add(10, 5);
let result2 = calculator::subtract(10, 5);

println!("Addition: {}", result1);
println!("Subtraction: {}", result2);
}

Output:

Addition: 15
Subtraction: 5

3. Understanding the pub Keyword

In Rust, items inside a module are private by default.

The pub keyword makes an item accessible from outside its module.

Example:

mod student {
pub fn show_details() {
println!("Student details");
}

fn calculate_marks() {
println!("Calculating marks");
}
}

fn main() {
student::show_details();

// This will cause an error:
// student::calculate_marks();
}

Important:


Keyword

Meaning

mod

Declares a module

pub

Makes an item public

No pub

Item is private by default

A child module can access private items of its ancestors, but a parent cannot directly access private items declared inside its child module.

4. Creating Modules in Separate Files

In real-world applications, we usually create separate files for different modules.

Let's create a project named student_app.

Project structure:

student_app/
│
├── Cargo.toml
│
└── src/
├── main.rs
└── student.rs

Step 1: Create the student.rs file

Inside src/student.rs, write:

pub fn show_details() {
println!("Student Name: Hemant");
println!("Course: Rust Programming");
}

pub fn show_fee() {
println!("Course Fee: 5000");
}

Step 2: Declare the module in main.rs

mod student;

fn main() {
student::show_details();
student::show_fee();
}

Output:

Student Name: Hemant
Course: Rust Programming
Course Fee: 5000

Explanation:

  1. mod student; tells Rust to load the module from student.rs.
  2. student::show_details() calls the function inside the module.
  3. pub allows these functions to be accessed from main.rs.

You do not need to write the complete module code inside main.rs.

5. Understanding Module Paths

A module path tells Rust where an item is located.

Rust uses :: to separate module names and item names.

Example:

mod school {
pub mod student {
pub fn show_name() {
println!("Student: Hemant");
}
}
}

fn main() {
school::student::show_name();
}

Output:

Student: Hemant

The path is:

school → student → show_name()

6. Using the use Keyword

Writing a long module path repeatedly can make code difficult to read.

The use keyword brings a name into the current scope.

Example without use:

mod student {
pub fn show_name() {
println!("Hemant");
}
}

fn main() {
student::show_name();
student::show_name();
}

Example with use:

mod student {
pub fn show_name() {
println!("Hemant");
}
}

use student::show_name;

fn main() {
show_name();
show_name();
}

Output:

Hemant
Hemant

You can also import an entire module:

use student;

fn main() {
student::show_name();
}

7. Using self and super

Rust provides special keywords for navigating module paths.

self

self refers to the current module.

mod school {
pub fn show_school() {
println!("Capscom Technology");
}

pub mod student {
pub fn show_student() {
super::show_school();
println!("Student: Hemant");
}
}
}

fn main() {
school::student::show_student();
}

Output:

Capscom Technology
Student: Hemant

super

super refers to the parent module.

In the example above, super::show_school() accesses the function in the parent school module.

8. Creating Nested Modules in Separate Files

You can also organize modules inside folders.

Project structure:

student_app/
│
└── src/
├── main.rs
└── school/
├── mod.rs
└── student.rs

main.rs

mod school;

fn main() {
school::student::show_name();
}

school/mod.rs

pub mod student;

school/student.rs

pub fn show_name() {
println!("Student: Hemant");
}

Output:

Student: Hemant

Here:

  1. main.rs declares the school module.
  2. school/mod.rs declares the student submodule.
  3. school/student.rs contains the student-related function.

Note: Modern Rust also supports school.rs instead of school/mod.rs for declaring the parent module. Both approaches are valid.

9. What Are Packages and Crates?

Rust has three important concepts: Package, Crate, and Module.

Package

A package is a project managed by Cargo.

It contains a Cargo.toml file and one or more crates.

Crate

A crate is the unit of compilation in Rust.

There are two main types:

  1. Binary crate – An executable application.
  2. Library crate – A library that other code can use.

Module

A module organizes code inside a crate.

Comparison:


Concept

Purpose

Package

Manages a Rust project

Crate

Compilation unit

Module

Organizes code inside a crate

Common crate root files


File

Purpose

src/main.rs

Root of a binary crate

src/lib.rs

Root of a library crate

A package can contain both a binary crate and a library crate.

10. Using as to Rename Imports

You can rename an imported item using the as keyword.

Example:

mod student {
pub fn show_name() {
println!("Hemant");
}
}

use student::show_name as display_name;

fn main() {
display_name();
}

Output:

Hemant

Here, show_name is imported with the new name display_name.

11. Using pub use to Re-export Items

Sometimes you want to make an item available through another module path.

Rust provides pub use for this purpose.

Example:

mod student {
pub fn show_name() {
println!("Hemant");
}
}

pub use student::show_name;

fn main() {
show_name();
}

Output:

Hemant

Here, pub use re-exports the function so it can be accessed through the current module's public interface.

12. Common Mistakes


Mistake

Explanation

Forgetting mod student;

Rust does not automatically include the separate module file

Forgetting pub

An item may not be accessible from outside its module

Using the wrong path

Check the module hierarchy

Confusing mod and use

mod declares a module; use brings a path into scope

Incorrect file structure

Module file names and folder locations must match the declarations