Module

Module in Rust is nothing, but a group of code which can be compiled into a single unit called crate. There are two different types of crate in Rust. One is the binary crate which contains main() function, acting as the entry point for the execution of the program. Another is the library crate which is inside the Rust library, and does not have any specific entry point.

The binary crate is a simple program module that can be only executed for single-purpose use. But the library crate can be reused in different programs and projects of Rust.

The tool called Cargo is used to manage library crate in Rust. There are different types of modules such as- network module, which will contain only the network-related library functions. Graphics module, which will contain only graphics-elated library functions, etc.

There are third party crates available on the internet and can be downloaded from: https://crates.io/

Declaration of modules:

There are two different types of modules.

  • Private Module : Private Module is one, whose components cannot be accessed by other functions or modules in the program. When a module is declared in the program, then by default the modules are declared as private in Rust.
  • Public Module : Public Module is the one, whose components can be accessed by another module. To make a module as Public, we need to use the keyword pub at the time of declaring the module.

Syntax for declaring a module

mod module_name {
fn function_name();
}

A module is declared using the keyword mod. Below is the way a module is declared:

mod my_private_module {
    fn my_private_function()
}

In the above syntax, my_private_module is the module which we have declared using the keyword mod. Inside the module, that is after the opening brace { , we have a declared a function called my_private_function() and end the scope of the module with the closing brace }.

It should be noted that, whenever a module is declared directly, then by default the module is Private. It means that any other module or functions cannot access the components of the module.

Also, the function which is declared inside the module is by default is a private function. And to make the module or the function inside it public, the keyword pub should be used in front of the module or function name, at the time of declaration.

Declaring the module and its function as public

pub mod my_private_module {
    pub fn my_private_function()
}

In the above syntax, we have seen that the keyword pub is written in front of the module as well as the function name. This makes it possible, to access the components of the public module, by another module.

Implementation of a module

To implement a module, the module should be declared at first, as shown above. Modules can be declared as Private or Public.

Now, let us see how modules and their functions can be defined and used.

A program to display a string using a module.

pub mod company {
    pub fn display(company_name: String) {
        println!("The name of the company is: {}", company_name);
    }
}
fn main() {
    company::display("Chercher.tech".to_string());
}

In the above program, we have declared a public module called company.

pub mod company

Then inside the module, we have created a public function called display(), which accepts a parameter of string type.

  pub fn display(company_name: String)

Then, inside the function main(), the display function is accessed using the module name.

company::display("Chercher.tech".to_string());

Program Output :

rust-module-public

Using the keyword use

The keyword use can be used, by mentioning beforehand, which module or which function is going to be used later inside the program. The keyword use is used to remove the overhead, of mentioning the module name and the function name at the time of using the function.

Syntax :

use module_name::function_name;

A program to print a name using use

pub mod company {
    pub fn display(company_name: String) {
        println!("The name of the company is: {}", company_name);
    }
}
use company::display;
fn main() {

    display("Chercher.tech".to_string());
}

In the above program, we have declared a public module called company. And inside the module, there is a function called display() which is also declared as public. And outside the module, before the start of the main() function, we have written- use company::display; .

It means, we are telling our compiler that, in the later part of the program, we shall be using the function display() which is inside the module company.

Program Output:

rust-module-use

Nested Modules

Nested modules are nothing but inserting a module into another module. There can be multiple levels of nesting modules.

To use the concept of the nested module, we can simply declare a module inside another module and so on. The last module, which is very inside will contain the function. Or even inside any level of nesting, we can have a function.

The function is accessed in a similar way as discussed above.

Syntax for Nested module :

pub mod module_1 {
    pub mod module_2 {
        pub mod module_3 {
            function()
        }
    }
}

In the syntax above, it is clearly visible that:

  • module_3 is declared inside module_2.
  • module_2 is declared inside the module_1
  • module_1 is the parent module.
  • The function() is declared inside the module_3, which is the last nested module.

A program using Nested module :

pub mod country {
    pub mod city {
        pub mod company {
            pub fn display_name(name: String) {
                println!("The name of the Company is {}", name);
            }
        }
    }
}
use country::city::company::display_name;
fn main() {
    display_name("Chercher.tech".to_string());
}

In the above program, the module city is declared inside the module country. And the module company is declared inside the module city. The function display_name is inside the module company, which is the very inner module.

When the function from the very inner module has to be accessed, then the path has to be written using the keyword use. as- use country::city::company::display_name;

The function could also be accessed without using the keyword as-

Program Output :

rust-nested-module

Library Crate

Library crate is a group of code, which can be created by us and can be used as a library. Library crate is useful as it can be used by other modules as well.

Below are the steps to create a Library crate:

  • Make a project folder.
  • Create two directories one for building the library and the other for running the library.
  • Include all the necessary folders and sub-folders inside the directories.
  • Build.
  • Run.
Make a project Folder:

The project folder can be created within the executing directory, with any name of choice. In my case, it is inside the C drive. C Drive->users->HP.

Create a folder called company-project.

Create two directories inside the folder:
  • company-lib
  • company-lib-test:

rust-crate-step-one

Now inside the folder company-lib create:

  • A folder called: src
  • A file called Cargo.toml.

rust-crate-step-two

Now inside the folder src, create two files:

  • companies.rs
  • lib.rs

rust-crate-step-three-refined

Add the following code inside the companies.rs file:

pub fn display(name: String) {
    println!("The company project is {} :companies-project", name);
}

And the following code inside the lib.rs file:

pub mod companies;

Now go to the command line interface and navigate to the company-lib directory. And then use the command Cargo build to build your first library crate:

rust-crate-cargo-build

Now, open the folder company-lib, and you must see that one target directory and one Cargo.lock file has been automatically created. This means, you have successfully built the crate.

rust-crate-step-four-redefined

Creating a Binary Crate

Navigate inside the folder company-lib-test and create:

  • A folder src.
  • A file called Cargo.toml

rust-crate-step-five

Inside the folder src , create a file called main.rs

rust-crate-step-six

Add the following code inside the file called main.rs:

extern crate companies_lib;
use companies_lib::companies::display;
fn main() {
    println!("This is the execution of the main function");
    display("Chercher.tech".to_string())
}

Add the below code to the Cargo.toml file:

[package]
name = "test_for_company_lib"
version = "0.1.0"
authors = ["KarthiQ"]
    [dependencies]
companies_lib = {
    path = "../company-lib"
}

Now, in the command-line interface navigate to the company-lib folder and type the command cargo run.

Output :

rust-crate-cargo-run

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions