30 days of Rust - Day Five - Modules
It feels really good to know that a few people read my blog posts π. I have no idea what I am doing, and so do many people out there. I have already heard 2 people text me letting me know that I have inspired them to take on the challenge of Learning a new programming language (namly Rust and Python). You can do it too. It doesn't even have to be programming. It can be at anything you want to improve at such as running, writing, reading, cooking, etc. The goal is to get outside of your comfort zone and be consistent. Show up even when you don't feel like it.
website improvements
I spent some time coding (In React not Rust π€£) new features trying to improve my website. I added;
-
Google Analytics in order to see if people at least read my posts..
-
Social media sharing icons on the right of your screen. I used React Social for Facebook, LinkedIn and Twitter. Please try them out.
-
Whatsapp share. I custom made this. If you'd like to implement it, it's quite easy. Check out their documentation here
Please like and share my Linkedin post. It really means a lot. Now let's get on to today's business.βοΈ
Day Five - Modules and Privacy
Today was a very confusing day, I learnt about crates, packages and modules. I understand these from a high level, but I must say the implementation gets quite dizzy.
As our programs get larger, they become harder to manage. To make our lives (and of those who read/contribute to our code) easier, we need to structure it into different modules.
A module is effectively a group of functions, types, traits, etc. which help us organize code in a better way. Similar to the file system on your computer (folders can contain other folders), Rust modules can also contain other modules.
Creating a module
In order to create a module, we need to use the mod
keyword.
mod car {
fn drive(){
println!("vrrrrphaaa ππ¨");
}
}
Visibility
Rust by design, makes everything private. Which means code which is out of scope
. This means variables and methods can only be accessed by others inside the current module or their children/descendents.
In order to make an item visible/accessible from anywhere, you need to use the pub
keyword. Below, this makes the main function have access to it as follows;
mod car {
pub fn drive(){
//do some stuff
combust_engine();
//turn the wheels
}
fn combust_engine(){
// i think pistons go up and down
println!("vrrrrphaaa ππ¨");
}
}
fn main(){
car::drive();
}
"But the car
function is private. How does main have access to it?". Well that is because car
and main
are siblings.
Imports
Currently we have our module in the same file as our main function. In order to make our code more readable we can have the car and its related code in their own file and folder.
Now in order for our main file to use drive
method, we simply bring car
into scope using the words mod car
mod car;
fn main(){
car::drive();
}
Also note; we cannot access the method combust_engine
. It is private, and for a good reason. If I want to go somewhere with my Golf GTI, I just want to drive
the car and I don't care about what happens under the hood. This is a concept known as encapsulation.
Conclusion
Modules allow us to have more structured code, by grouping related code together. On top of that it gives us the ability to only give access to certain parts of our code.
The learning curve for Rust is quite steep I must say. I'm taking way more than an hour per day now and this has by far been the most challenging concept.
Hopefully it shall get easier. Please let me know what you think of these posts and if you are joining me too on this challenge.
If you just joined, please have a look at my day one post to see how this journey is progressing.
Thank you so much for taking time out of your day to read this post. Until next time (tomorrowπ)