{"id":3664,"date":"2026-11-10T12:43:49","date_gmt":"2026-11-10T04:43:49","guid":{"rendered":"http:\/\/www.rbgcounseling.com\/blog\/?p=3664"},"modified":"2026-11-10T12:43:49","modified_gmt":"2026-11-10T04:43:49","slug":"how-to-use-a-module-in-rust-4d29-568d11","status":"publish","type":"post","link":"http:\/\/www.rbgcounseling.com\/blog\/2026\/11\/10\/how-to-use-a-module-in-rust-4d29-568d11\/","title":{"rendered":"How to use a module in Rust?"},"content":{"rendered":"<p>Hey folks, let\u2019s cut the crap\u2014if you\u2019re deep into Rust, you\u2019ve probably stared at a messy codebase where a single file\u2019s got 500 lines of random functions, and you\u2019re like \u201cwhere do I even start looking for that auth helper?\u201d Modules fix that. And since I\u2019m the guy who builds Rust modules for a living (yeah, I supply \u2019em, not just mess around with \u2019em), I\u2019m gonna walk you through how to use one the right way\u2014no jargon, no fluff, just stuff that actually works, especially if you\u2019re grabbling with third-party modules or even writing your own to make your team\u2019s life easier. <a href=\"https:\/\/www.uvledtek.com\/module\/\">Module<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.uvledtek.com\/uploads\/45220\/page\/small\/disinfection-light-panelcd94e.png\"><\/p>\n<p>First, let\u2019s get one thing straight: Rust\u2019s module system isn\u2019t the garbage thing some make it out to be. It\u2019s just strict. Which is good. It stops you from accidentally importing that messy utility function you meant to delete, right off the bat. Let\u2019s split this into two parts: using a module I supply (since that\u2019s why you\u2019re here, probably) and using a module you build for your own code. Fair?<\/p>\n<p>Let\u2019s start with the basics of <em>any<\/em> module, whether it\u2019s from me or your buddy\u2019s GitHub repo. Rust groups code into modules via the <code>mod<\/code> keyword, right? But when you\u2019re using an external module (like one of mine), you don\u2019t use <code>mod<\/code>\u2014you use <code>use<\/code>. Wait, hold on, don\u2019t skip that. A lot of new folks mix that up. <code>mod<\/code> is for internal modules in your own crate, <code>use<\/code> is for bringing external code (from crates.io, or from me) into your scope.<\/p>\n<p>Wait, let\u2019s take an example. Say I supply a <code>rust-auth-utils<\/code> module\u2014you know, the stuff that handles password hashing, JWT generation, rate limiting for login endpoints. Cool. So first step: you add it to your <code>Cargo.toml<\/code>, duh. That\u2019s where Rust keeps track of your dependencies. Just pop this line in: <code>rust-auth-utils = &quot;0.1.0&quot;<\/code> (I\u2019d send you the exact version when you reach out, no worries). Run <code>cargo build<\/code> and boom\u2014you\u2019ve got access to it.<\/p>\n<p>Now, how to actually use it in your code. Let\u2019s say you want to use the password hashing function, which is in <code>rust_auth_utils::hashing::hash_password<\/code> (yeah, Rust converts underscores in crate names to colons in imports, that\u2019s a quirk, but easy to remember). A lot of people do this: <code>use rust_auth_utils;<\/code> then call <code>rust_auth_utils::hashing::hash_password(...)<\/code>. That works, but it\u2019s extra typing. You can simplify it with a better <code>use<\/code> line: <code>use rust_auth_utils::hashing;<\/code> then call <code>hashing::hash_password(...)<\/code>. Or even <code>use rust_auth_utils::hashing::hash_password;<\/code> so you can just call <code>hash_password(...)<\/code> directly inside your functions. That\u2019s the sweet spot\u2014don\u2019t import more than you need, but don\u2019t make yourself type the whole path every time.<\/p>\n<p>Here\u2019s a common mistake I see, though: trying to access a private function. Rust modules have privacy rules, and they\u2019re non-negotiable. If a function or struct in my module is marked <code>pub<\/code>, you can use it. If it\u2019s not, you can\u2019t. I don\u2019t expose the messy internal parts of my modules\u2014why would you want to mess with the hashing algorithm\u2019s salt generation, right? So don\u2019t go digging for things not marked <code>pub<\/code>\u2014that\u2019s 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.<\/p>\n<p>Now, what if you\u2019re writing your own module, not just using mine? Let\u2019s say you\u2019ve got a <code>user_management.rs<\/code> file in your project. You can turn that into a module by adding <code>mod user_management;<\/code> in your <code>main.rs<\/code> or <code>lib.rs<\/code>. Then, if you want to use something from it, same <code>use<\/code> rule: if you make a function <code>pub fn create_user(...)<\/code> in <code>user_management.rs<\/code>, you can call it with <code>use crate::user_management::create_user;<\/code> and boom, it works. This is exactly how I structure my supply modules\u2014they\u2019re just crates, which are basically self-contained modules, so the same rules apply.<\/p>\n<p>Wait, let\u2019s test this with a real, trivial example so you don\u2019t get lost. Here\u2019s a tiny main.rs file:<\/p>\n<p>use rust_auth_utils::hashing::hash_password;<\/p>\n<p>fn main() {<br \/>\nlet my_pass = &quot;supersecret123&quot;;<br \/>\nlet hashed = hash_password(my_pass);<br \/>\nprintln!(&quot;Your hashed password: {}&quot;, hashed);<br \/>\n}<\/p>\n<p>That\u2019s it. No extra stuff. Run <code>cargo run<\/code> and it works. No compilation errors, no missing imports, because you did it right. Now, if you messed up the import\u2014like wrote <code>use rust_auth_utils::hash_password;<\/code>\u2014Rust would yell at you, because <code>hash_password<\/code> is inside the <code>hashing<\/code> submodule of my crate. That\u2019s the strictness I was talking about, and it\u2019s a good thing. It stops typos and confusion.<\/p>\n<p>Now, let\u2019s talk about scoping. A lot of new devs think <code>use<\/code> makes something global, but no\u2014imports are scoped to the file they\u2019re written in. If I add that <code>use<\/code> line in main.rs, I can only use <code>hash_password<\/code> in main.rs. If I want to use it in another file, say <code>login.rs<\/code>, I have to add the same <code>use<\/code> line there too. Or, if I put the import in <code>lib.rs<\/code> (if you\u2019re building a library) and mark it <code>pub use<\/code>, then anyone using your crate can import it from there. That\u2019s how I make my modules easy to use\u2014all the common exports are in the top-level lib.rs, so you don\u2019t have to dig through submodules.<\/p>\n<p>Wait, what if you want to re-export a part of my module to make it easier for your team? Let\u2019s say your whole project uses my <code>hash_password<\/code> all the time. You can make a local module in your own crate, say <code>pub mod auth { pub use rust_auth_utils::hashing::hash_password; }<\/code>, then your team can just do <code>use your_crate::auth::hash_password;<\/code> instead of typing the whole <code>rust_auth_utils::hashing<\/code> path every time. That\u2019s a pro tip I give all my clients\u2014simplify imports for your team, don\u2019t make them repeat the whole thing.<\/p>\n<p>Now, let\u2019s cover a gotcha that trips up everyone: modules and <code>Cargo<\/code>. If you\u2019re using a module from me, I\u2019ll send you the exact version to put in <code>Cargo.toml<\/code>\u2014never use a version with <code>*<\/code> unless you know what you\u2019re doing, because that can break your code when I push an update. Stick to exact minor versions, like <code>0.1.4<\/code> instead of <code>0.1.*<\/code> until you\u2019ve tested the update. I test all my modules before releasing, so my clients don\u2019t get unexpected breaks, but it\u2019s still better to pin the version until you\u2019re ready to update.<\/p>\n<p>Another thing: if you\u2019re using a module I supply, I include documentation right in the crate, so you can run <code>cargo doc --open<\/code> and see all the functions, their parameters, and examples. No guessing what a function does\u2014check the docs. I make sure every exported function has a simple example, so you don\u2019t have to email me asking \u201chow do I use this rate limiter?\u201d Just run the docs and it\u2019s right there.<\/p>\n<p>Now, let\u2019s talk about when to use a module from me vs. writing your own. If you\u2019re building auth for a web app, don\u2019t waste time writing your own password hashing or JWT logic. That\u2019s what I\u2019m for\u2014modules that are tested, secure, and follow Rust\u2019s best practices. I eliminate the \u201croll your own crypto\u201d 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.<\/p>\n<p>Wait, let\u2019s circle back to the core question: how do you actually use a module, step by step, no mistakes? Let\u2019s make a quick checklist:<\/p>\n<ol>\n<li>Add the module to your <code>Cargo.toml<\/code> (if it\u2019s external). If it\u2019s internal, create the .rs file and add <code>mod module_name;<\/code> in your main\/lib file.<\/li>\n<li>Find the exported items in the module (look for <code>pub<\/code> in the docs or code).<\/li>\n<li>Write a <code>use<\/code> line that brings the exact item into your scope (don\u2019t import the whole module unless you need to).<\/li>\n<li>Call the function\/struct in your code, test it with <code>cargo run<\/code>.<\/li>\n<li>If something breaks, check the privacy rule first\u2014you\u2019re probably trying to use something not marked <code>pub<\/code>.<\/li>\n<\/ol>\n<p>That\u2019s it. No fancy tricks, no complex stuff. Rust\u2019s module system is straightforward once you stop overcomplicating it.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.uvledtek.com\/uploads\/45220\/page\/small\/static-water-module3ac0c.png\"><\/p>\n<p>Now, if you\u2019re someone who\u2019s tired of fixing messy code, or you don\u2019t want to spend weeks building a secure module from scratch, hit me up to talk about my modules. I build Rust modules for everything\u2014auth, data parsing, file handling, even stuff like game dev utilities. They\u2019re lightweight, well-documented, and follow all Rust\u2019s rules so you don\u2019t 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.<\/p>\n<p><a href=\"https:\/\/www.uvledtek.com\/module\/\">Module<\/a> Now, the references for all the Rust stuff I talked about\u2014these are the official Rust docs, so don\u2019t bother Googling sketchy random blogs:<\/p>\n<ul>\n<li>Rust Programming Language Book (Chapters 7: Modules, 14: Cargo and Crates.io)<\/li>\n<li>Rust API Guidelines (Module and Visibility Rules)<\/li>\n<li>Rust Reference (Module System Specification)<\/li>\n<\/ul>\n<hr>\n<p><a href=\"https:\/\/www.uvledtek.com\/\">UVLEDTEK Group<\/a><br \/>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.<br \/>Address: Huangshi Industrial Zone, Putian City, Fujian Province, China<br \/>E-mail: info@uvledtek.com<br \/>WebSite: <a href=\"https:\/\/www.uvledtek.com\/\">https:\/\/www.uvledtek.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey folks, let\u2019s cut the crap\u2014if you\u2019re deep into Rust, you\u2019ve probably stared at a messy &hellip; <a title=\"How to use a module in Rust?\" class=\"hm-read-more\" href=\"http:\/\/www.rbgcounseling.com\/blog\/2026\/11\/10\/how-to-use-a-module-in-rust-4d29-568d11\/\"><span class=\"screen-reader-text\">How to use a module in Rust?<\/span>Read more<\/a><\/p>\n","protected":false},"author":103,"featured_media":3664,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3627],"class_list":["post-3664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-module-4bbd-56bc4f"],"_links":{"self":[{"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/posts\/3664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/users\/103"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/comments?post=3664"}],"version-history":[{"count":0,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/posts\/3664\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/posts\/3664"}],"wp:attachment":[{"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/media?parent=3664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/categories?post=3664"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rbgcounseling.com\/blog\/wp-json\/wp\/v2\/tags?post=3664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}