Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- VueJS
- Go
Practical Paper
Industrial Training
Rust Modules
A module is a namespace which contains the definitions of the functions or its types. A module is a collection of items such as functions, structs, traits, impl blocks. By default, the modifier of the module is private, but it can be overridden with the public modifier by using pub keyword.
Following are the keywords used in the modules:
-
mod keyword: The "mod" keyword declares the new module.
pub keyword: By default, all the functions, types, modules and constants have a private visibility modifier. The pub keyword makes the visibility modifier as public, and therefore, they are accessible outside the namespace.
use keyword: The use keyword is used to import the module into local scope.
Module Definition
The module is defined by the mod keyword.
The syntax of Module:
mod module_name // body inside the module.
A Module can be categorized in three ways:
1. Single module: When the module appeared in a single file is known as a single module.
Let's understand this through an example:
mod a { pub fn single_module() { println!("Single module"); } } fn main() { a::single_module(); }
Output:
Single module
In the above example, module 'a' is defined, and every code defined in the block is inside the namespace 'a'. The function of module 'a' can be called by using the module name followed by namespace and then function name.
-
We can also do the above example by using a separate file:
mod module; fn main() { module::single_module(); }
pub fn single_module() { println!("Single module"); }
Output:
Single module
In the above two examples, we examine that mod X is defined either in curly braces or in a separate file named as X.rs or X/mod.rs.
2. Sub-modules: In a single file, we can have multiple modules. Suppose the library name is "language" and it consists of two modules, i.e., C and Cplus.
Hierarchy of a "language" library is given below:
Let's understand through an example:
mod c { pub fn c() { println!("C is a structured programming language"); } } mod cplus { pub fn cplus() { println!("C++ is an object-oriented programming language"); } } fn main() { c::c(); cplus::cplus(); }
Output:
C is a structured programming language C++ is an object-oriented programming language
In the above example, the program consists of two modules, i.e., c and cplus and their respective functions are called by using c::c() and cplus::cplus().
3. Nested modules: Nested modules are those modules which consist of a module inside of modules, and they can be useful when the related modules are grouped together.
Let's understand this through an example:
mod a { pub fn a() { println!("a module"); } pub mod b { pub fn a() { println!("b module"); } } } fn main() { a::a(); a::b::b(); }
Output:
a module b module
In the above example, the program consists of two modules, i.e., 'a' and 'b' where 'b' is the inner module of 'a'. Both the modules consist the function with the same name but with different functionality. Both the functions are called by using a::a() and a::b::b() respectively. They both will not conflict with each other as they belong to different namespaces.