Hello! In this lesson, we will explore an essential aspect of Rust programming and OOP — modules and encapsulation. This lesson welcomes you into the world of Rust's modularity and the principles of encapsulation, key features in writing maintainable and scalable code.
Modules in Rust help organize code into separate namespaces, making it easier to manage and navigate larger projects. Encapsulation allows you to restrict access to parts of your code, promoting safer and more intentional interactions with your data structures.
In this lesson, we will:
- Introduce the concept and syntax of modules
- Learn to control visibility with
pub - Implement encapsulated data using
structmethods
Let's dive in!
Modules in Rust are like containers that organize your functions, structs, traits, and methods. They allow you to create structs and methods while using encapsulation to control access to a structs methods and fields.
Here's a simple example to create a module in Rust:
In this example:
- We defined a module named
bankusing themodkeyword - Inside the
bankmodule, we created aBankAccountstruct with two fields
Now that we have created a module, let's explore how to use it! By default, any structs or methods defined in a module are private and cannot be accessed by code outside the module. To allow access to code inside the module, we use the pub keyword, making the code public. In this section we make the BankAccount public, create a public constructor, and create a new BankAccount instance.
- Adding the
pubkeyword to theBankAccountstruct allows code in themainfunction to access thestruct - We added a public associated function for the
BankAccountstruct callednewthat creates a new instance of aBankAccount - Inside
main, we created an instance of aBankAccount - The syntax to access the
newmethod is<module name>::<struct name>::<struct method>(<input parameters>)
Let's take a deeper dive into encapsulation with the pub keyword. Continuing with the BankAccount example, currently both fields of the struct are private.
We want users to be allowed to check the balance and deposit money, but not allow them to directly access the balance field. To do this, we will create a public deposit method to change the balance field and a public get_balance method.
Additionally, we want the name associated with the account to be accessed by anyone, so we make it public. Let's take a look:
Let's break this code down step by step.
- Inside the
bankmodule, we've changed thenamefield to be public - We've created a public method
depositthat takes in a mutable reference toself
To make the importance of encapsulation more clear we will allow users to withdraw money, ensuring the account balance does not become negative.
Adding onto the code from the previous section:
The withdraw method ensures that amount can be withdrawn from the balance without the account becoming negative.
The code shows the successful withdrawal of $300.50 and an unsuccessful withdrawal of $2000.
The attempt to directly modify my_account.balance causes an error because the balance field is private. If users could directly modify the balance field, they could withdraw more money than they actually have.
Great job! You've learned how to create and use modules, control access with pub, and apply encapsulation in Rust. These skills are crucial for organizing your code and enhancing its readability, safety, and maintainability.
Now it’s time to practice what you’ve learned. Get ready to dive into the exercises and reinforce your understanding of modules and encapsulation in Rust.
