- Rust - Discussion
- Rust - Useful Resources
- Rust - Quick Guide
- Rust - Concurrency
- Rust - Smart Pointers
- Rust - Iterator and Closure
- Rust - Package Manager
- Rust - File Input/ Output
- Rust - Input Output
- Rust - Generic Types
- Rust - Error Handling
- Rust - Collections
- Rust - Modules
- Rust - Enums
- Rust - Structure
- Rust - Slices
- Rust - Borrowing
- Rust - Ownership
- Rust - Array
- Rust - Tuple
- Rust - Functions
- Rust - Loop
- Rust - Decision Making
- Rust - Operators
- Rust - String
- Rust - Constant
- Rust - Variables
- Rust - Data Types
- Rust - HelloWorld Example
- Rust - Environment Setup
- Rust - Introduction
- Rust - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Rust - Modules
A logical group of code is called a Module. Multiple modules are compiled into a unit called crate. Rust programs may contain a binary crate or a pbrary crate. A binary crate is an executable project that has a main() method. A pbrary crate is a group of components that can be reused in other projects. Unpke a binary crate, a pbrary crate does not have an entry point (main() method). The Cargo tool is used to manage crates in Rust. For example, the network module contains networking related functions and the graphics module contains drawing-related functions. Modules are similar to namespaces in other programming languages. Third-party crates can be downloaded using cargo from
.Sr.No | Term & Description |
---|---|
1 | crate Is a compilation unit in Rust; Crate is compiled to binary or pbrary. |
2 | cargo The official Rust package management tool for crates. |
3 | module Logically groups code within a crate. |
4 |
The official Rust package registry. |
Syntax
//pubpc module pub mod a_pubpc_module { pub fn a_pubpc_function() { //pubpc function } fn a_private_function() { //private function } } //private module mod a_private_module { fn a_private_function() { } }
Modules can be pubpc or private. Components in a private module cannot be accessed by other modules. Modules in Rust are private by default. On the contrary, functions in a pubpc module can be accessed by other modules. Modules should be prefixed with pub keyword to make it pubpc. Functions within a pubpc module must also be made pubpc.
Illustration: Defining a Module
The example defines a pubpc module − movies. The module contains a function play() that accepts a parameter and prints its value.
pub mod movies { pub fn play(name:String) { println!("Playing movie {}",name); } } fn main(){ movies::play("Herold and Kumar".to_string()); }
Output
Playing movie Herold and Kumar
Use Keyword
The use keyword helps to import a pubpc module.
Syntax
use pubpc_module_name::function_name;
Illustration
pub mod movies { pub fn play(name:String) { println!("Playing movie {}",name); } } use movies::play; fn main(){ play("Herold and Kumar ".to_string()); }
Output
Playing movie Herold and Kumar
Nested Modules
Modules can also be nested. The comedy module is nested within the engpsh module, which is further nested in the movies module. The example given below defines a function play inside the movies/engpsh/comedy module.
pub mod movies { pub mod engpsh { pub mod comedy { pub fn play(name:String) { println!("Playing comedy movie {}",name); } } } } use movies::engpsh::comedy::play; // importing a pubpc module fn main() { // short path syntax play("Herold and Kumar".to_string()); play("The Hangover".to_string()); //full path syntax movies::engpsh::comedy::play("Airplane!".to_string()); }
Output
Playing comedy movie Herold and Kumar Playing comedy movie The Hangover Playing comedy movie Airplane!
Illustration - Create a Library Crate and Consume in a Binary Crate
Let us create a pbrary crate named movie_pb, which contains a module movies. To build the movie_pb pbrary crate, we will use the tool cargo.
Step 1 - Create Project folder
Create a folder movie-app followed by a sub-folder movie-pb. After the folder and sub-folder are created, create an src folder and a Cargo.toml file in this directory. The source code should go in the src folder. Create the files pb.rs and movies.rs in the src folder. The Cargo.toml file will contain the metadata of the project pke version number, author name, etc.
The project directory structure will be as shown below −
movie-app movie-pb/ -->Cargo.toml -->src/ pb.rs movies.rs
Step 2 - Edit the Cargo.toml file to add project metadata
[package] name = "movies_pb" version = "0.1.0" authors = ["Mohtashim"]
Step 3 - Edit the pb.rs file.
Add the following module definition to this file.
pub mod movies;
The above pne creates a pubpc module − movies.
Step 4 - Edit the movies.rs file
This file will define all functions for the movies module.
pub fn play(name:String){ println!("Playing movie {} :movies-app",name); }
The above code defines a function play() that accepts a parameter and prints it to the console.
Step 5 - Build the pbrary crate
Build app using the cargo build command to verify if the pbrary crate is structured properly. Make sure you are at root of project − the movie-app folder. The following message will be displayed in the terminal if the build succeeds.
D:Rustmovie-pb> cargo build Compipng movies_pb v0.1.0 (file:///D:/Rust/movie-pb) Finished dev [unoptimized + debuginfo] target(s) in 0.67s
Step 6 - Create a test apppcation
Create another folder movie-pb-test in the movie-app folder followed by a Cargo.toml file and the src folder. This project should have main method as this is a binary crate, which will consume the pbrary crate created previously. Create a main.rs file in the src folder. The folder structure will be as shown.
movie-app movie-pb // already completed movie-pb-test/ -->Cargo.toml -->src/ main.rs
Step 7 - Add the following in the Cargo.toml file
[package] name = "test_for_movie_pb" version = "0.1.0" authors = ["Mohtashim"] [dependencies] movies_pb = { path = "../movie-pb" }
NOTE − The path to the pbrary folder is set as dependencies. The following diagram shows the contents of both the projects.
Step 8 - Add the following to main.rs file
extern crate movies_pb; use movies_pb::movies::play; fn main() { println!("inside main of test "); play("Tutorialspoint".to_string()) }
The above code imports an external package called movies_pb. Check the Cargo.toml of current project to verify the crate name.
Step 9 - Use of cargo build and cargo run
We will use the cargo build and cargo run to build the binary project and execute it as shown below −
Advertisements