#3: Importing Modules in Rust

Now that we have defined our storage.rs module, let's import it in our main module and test it with some data.

There are many ways we can do an import, let's try a simple way for now.

mod storage;
use storage::Database;

I used the mod keyword to tell Rust to make it available in my namespace, followed by the use keyword which imports our Database class that we defined earlier.

Let's say I want to make a database that only has data about me. First we need to make an instance of our database. The following blocks of code will live in our main function.

let mut db = Database::new();

As you can see, we use the let keyword to define a constant in Rust. I use the word "constant" on purpose because that's what it really is. In Rust by default all "variables" are constant and not the other way around.

In this case, however, we know we will be making changes to our db instance (i.e adding or removing data). So, we explicitly add a mut after the let keyword to tell Rust it's a mutable value. Then we store an instance of our database in this variable by calling the Database::new(); .

 

This article was updated on January 6, 2025

I am full stack software engineer, mainly focused on building the modern web. I started my journey by building in Visual Basic in high school, built agricultural AI systems in college and then fell in love with web development.

Now I am writing this blog as I learn Rust :)