If you are new to rust, I would strongly suggest you checkout rust docs at rust they are pretty great.
Modules are one of the better ways to organize your code and reuse it wherever it is necessary.
Rust module system is completely different from what I've used in Languages like JavaScript, GoLang and Python. When I started to learn this pattern, I didn't really like it but, after using them for sometime, now I really like the way it works.
you can find sample code here.
First we will see how to install external modules (crates). To install, all you have to do is goto Cargo.toml add crate name and run you project with command cargo run. once you run this command, cargo will install crate to you project. your Cargo.toml probably might looks like below.
[package]
name = "rust-mod-example"
version = "0.1.0"
authors = ["saiumesh"]
edition = "2018"
[dependencies]
uuid = { version = "0.7", features = ["serde", "v4"] }
now use that crate, may be as below 😁
// main.rs
// this is external crate, which we installed via cargo.toml
use uuid::Uuid;
fn get_uuid() -> String {
Uuid::new_v4().to_string()
}
fn main() {
let uuid = get_uuid();
println!("the uuid is {}", uuid);
}
now let's create a local module and see how we use it.
Before, we write any further code, there are two ways to create mods in rust,
you think in a way that each file and folder is mod in rust.
first we create mod with file
// filemod.rs
use uuid::Uuid;
// pub indicates that, we access this function outside of it's self mod
pub fn get_uud() -> String {
Uuid::new_v4().to_string()
}
// this is private function which cannot be accessed outside
fn cannot_access() {
println!("I cannot be called!!");
}
// now in main.rs we will import/define mod by as following
// now, we have created file called 'filemod'
// which is local mod goes by filename
mod filemod;
use filemod::get_uud;
fn main() {
// local module which goes by filename
let file_mod_uuid = get_uud();
println!("local module uuid is {}", file_mod_uuid);
}
now let's crate a mod, with Folder
as discussed, folder can be a mod, only with slight difference.
when we create mod with folder, that folder should have file called mod.rs
which helps rust to identify this folder as one of the modules.
Folder/{mod.rs, filename.rs}
This might be little confusion but, trust once you get little familiar with it I'm sure you gonna love it.
Now let's create folder called foldermodule, in that folder fist we create mod.rs and then create foldermodulefile.rs
foldermodule/mod.rs
// now make file module public
pub mod foldermodulefile;
foldermodule/foldermodulefile.rs
use uuid::Uuid;
pub fn folder_module_uuid() -> String {
Uuid::new_v4().to_string()
}
Now let's import this module in main
// now , we have created folder called 'foldermodule'
// which is local mod goes by module called 'foldermodule'
mod foldermodule;
// you use following syntax for importing multiple functions from same mod
use foldermodule::foldermodulefile::{ folder_module_uuid, mod_to_mod_function };
fn main() {
// local folder module
let folder_mod_uuid = folder_module_uuid();
println!("folder module uuid is {}", folder_mod_uuid);
}
Till now we have how to use file/folder modules in main, now let's see how to call a mod function from other mod
now let's create mod
//modtomod/mod.rs
pub mod something;
//modtomod/something.rs
pub fn mod_to_mod() -> String {
String::from("Hey!! mod")
}
calling mod function from other mod, is little different, you cannot just write use modulename::function_name that would throw out of scope error.
since main.rs binds everything together, you need to define every module in main.rs as below.
main.rs
// till now we have seen using local modules in main file
// now let's see how to do mod to mod rather mod to main
// to use mod in other mod first we need to define called mod in
// main, so that caller mod can have it's scope
mod modtomod;
Now let's go our foldermodule mods call other mod functions
to call one mod function from other module we will use following syntax.
// now let's call other local mod
use modtomod::something; 😢😢
// now let's call other local mod
use crate::modtomod::something; 😁😁
the you call the same function as following code
// now let's call other local mod
use crate::modtomod::something;
pub fn mod_to_mod_function() {
println!("hey!! from other mod {}", something::mod_to_mod());
}
Hopefully I made some sense about modules in Rust Language. If you have any doubts or suggestions you can contact me on my twitter. until then, I will see you in other Post. Peace ❤❤
Top comments (4)
Wish that I had found this sooner! One night wasted trying to understand what the docs are telling me. Not that the docs are bad, I'm just not a good doc reader. 🤦♂️
I'm glad you liked.
Great Idea. Would you be interested if I create a repo on it ?