Posted in

How to use a module in Rust?

Hey folks, let’s cut the crap—if you’re deep into Rust, you’ve probably stared at a messy codebase where a single file’s got 500 lines of random functions, and you’re like “where do I even start looking for that auth helper?” Modules fix that. And since I’m the guy who builds Rust modules for a living (yeah, I supply ’em, not just mess around with ’em), I’m gonna walk you through how to use one the right way—no jargon, no fluff, just stuff that actually works, especially if you’re grabbling with third-party modules or even writing your own to make your team’s life easier. Module

First, let’s get one thing straight: Rust’s module system isn’t the garbage thing some make it out to be. It’s just strict. Which is good. It stops you from accidentally importing that messy utility function you meant to delete, right off the bat. Let’s split this into two parts: using a module I supply (since that’s why you’re here, probably) and using a module you build for your own code. Fair?

Let’s start with the basics of any module, whether it’s from me or your buddy’s GitHub repo. Rust groups code into modules via the mod keyword, right? But when you’re using an external module (like one of mine), you don’t use mod—you use use. Wait, hold on, don’t skip that. A lot of new folks mix that up. mod is for internal modules in your own crate, use is for bringing external code (from crates.io, or from me) into your scope.

Wait, let’s take an example. Say I supply a rust-auth-utils module—you know, the stuff that handles password hashing, JWT generation, rate limiting for login endpoints. Cool. So first step: you add it to your Cargo.toml, duh. That’s where Rust keeps track of your dependencies. Just pop this line in: rust-auth-utils = "0.1.0" (I’d send you the exact version when you reach out, no worries). Run cargo build and boom—you’ve got access to it.

Now, how to actually use it in your code. Let’s say you want to use the password hashing function, which is in rust_auth_utils::hashing::hash_password (yeah, Rust converts underscores in crate names to colons in imports, that’s a quirk, but easy to remember). A lot of people do this: use rust_auth_utils; then call rust_auth_utils::hashing::hash_password(...). That works, but it’s extra typing. You can simplify it with a better use line: use rust_auth_utils::hashing; then call hashing::hash_password(...). Or even use rust_auth_utils::hashing::hash_password; so you can just call hash_password(...) directly inside your functions. That’s the sweet spot—don’t import more than you need, but don’t make yourself type the whole path every time.

Here’s a common mistake I see, though: trying to access a private function. Rust modules have privacy rules, and they’re non-negotiable. If a function or struct in my module is marked pub, you can use it. If it’s not, you can’t. I don’t expose the messy internal parts of my modules—why would you want to mess with the hashing algorithm’s salt generation, right? So don’t go digging for things not marked pub—that’s a one-way ticket to broken code when I update the module. I design my modules to expose only what you need, so stick to the exported stuff.

Now, what if you’re writing your own module, not just using mine? Let’s say you’ve got a user_management.rs file in your project. You can turn that into a module by adding mod user_management; in your main.rs or lib.rs. Then, if you want to use something from it, same use rule: if you make a function pub fn create_user(...) in user_management.rs, you can call it with use crate::user_management::create_user; and boom, it works. This is exactly how I structure my supply modules—they’re just crates, which are basically self-contained modules, so the same rules apply.

Wait, let’s test this with a real, trivial example so you don’t get lost. Here’s a tiny main.rs file:

use rust_auth_utils::hashing::hash_password;

fn main() {
let my_pass = "supersecret123";
let hashed = hash_password(my_pass);
println!("Your hashed password: {}", hashed);
}

That’s it. No extra stuff. Run cargo run and it works. No compilation errors, no missing imports, because you did it right. Now, if you messed up the import—like wrote use rust_auth_utils::hash_password;—Rust would yell at you, because hash_password is inside the hashing submodule of my crate. That’s the strictness I was talking about, and it’s a good thing. It stops typos and confusion.

Now, let’s talk about scoping. A lot of new devs think use makes something global, but no—imports are scoped to the file they’re written in. If I add that use line in main.rs, I can only use hash_password in main.rs. If I want to use it in another file, say login.rs, I have to add the same use line there too. Or, if I put the import in lib.rs (if you’re building a library) and mark it pub use, then anyone using your crate can import it from there. That’s how I make my modules easy to use—all the common exports are in the top-level lib.rs, so you don’t have to dig through submodules.

Wait, what if you want to re-export a part of my module to make it easier for your team? Let’s say your whole project uses my hash_password all the time. You can make a local module in your own crate, say pub mod auth { pub use rust_auth_utils::hashing::hash_password; }, then your team can just do use your_crate::auth::hash_password; instead of typing the whole rust_auth_utils::hashing path every time. That’s a pro tip I give all my clients—simplify imports for your team, don’t make them repeat the whole thing.

Now, let’s cover a gotcha that trips up everyone: modules and Cargo. If you’re using a module from me, I’ll send you the exact version to put in Cargo.toml—never use a version with * unless you know what you’re doing, because that can break your code when I push an update. Stick to exact minor versions, like 0.1.4 instead of 0.1.* until you’ve tested the update. I test all my modules before releasing, so my clients don’t get unexpected breaks, but it’s still better to pin the version until you’re ready to update.

Another thing: if you’re using a module I supply, I include documentation right in the crate, so you can run cargo doc --open and see all the functions, their parameters, and examples. No guessing what a function does—check the docs. I make sure every exported function has a simple example, so you don’t have to email me asking “how do I use this rate limiter?” Just run the docs and it’s right there.

Now, let’s talk about when to use a module from me vs. writing your own. If you’re building auth for a web app, don’t waste time writing your own password hashing or JWT logic. That’s what I’m for—modules that are tested, secure, and follow Rust’s best practices. I eliminate the “roll your own crypto” mistake, which is the #1 way people break their apps. So reach out when you need a module that works out of the box, no debugging required.

Wait, let’s circle back to the core question: how do you actually use a module, step by step, no mistakes? Let’s make a quick checklist:

  1. Add the module to your Cargo.toml (if it’s external). If it’s internal, create the .rs file and add mod module_name; in your main/lib file.
  2. Find the exported items in the module (look for pub in the docs or code).
  3. Write a use line that brings the exact item into your scope (don’t import the whole module unless you need to).
  4. Call the function/struct in your code, test it with cargo run.
  5. If something breaks, check the privacy rule first—you’re probably trying to use something not marked pub.

That’s it. No fancy tricks, no complex stuff. Rust’s module system is straightforward once you stop overcomplicating it.

Now, if you’re someone who’s tired of fixing messy code, or you don’t want to spend weeks building a secure module from scratch, hit me up to talk about my modules. I build Rust modules for everything—auth, data parsing, file handling, even stuff like game dev utilities. They’re lightweight, well-documented, and follow all Rust’s rules so you don’t have to worry about mistakes. No hidden fees, no complicated contracts, just modules that work when you need them. I can send you a sample to test, no strings attached, before you commit to anything.

Module Now, the references for all the Rust stuff I talked about—these are the official Rust docs, so don’t bother Googling sketchy random blogs:

  • Rust Programming Language Book (Chapters 7: Modules, 14: Cargo and Crates.io)
  • Rust API Guidelines (Module and Visibility Rules)
  • Rust Reference (Module System Specification)

UVLEDTEK Group
As one of the most professional module manufacturers and suppliers in China, our products have good reputation in the market. Please rest assured to buy high quality module at competitive price from our factory. For more information, contact us now.
Address: Huangshi Industrial Zone, Putian City, Fujian Province, China
E-mail: info@uvledtek.com
WebSite: https://www.uvledtek.com/