Rust Modules In Less Than 5 Minutes

The View at 30,000 Feet

  1. Embedded in an existing source file.
  2. As a separate file next to your lib.rs or main.rs
  3. As a separate folder leveraging a mod.rs file.

Embedded Modules

Module Files

Module Folders

  1. The mod doorman; declaration in main.rs tells the Rust compiler that there’s a module named doorman somewhere in the src directory.
  2. The compiler will find the doorman directory and the mod.rs file inside of it, loading that file as the doorman module.
  3. Inside mod.rs we find two more mod declarations, pub mod greeter; and pub mod closer; . The compiler again looks for modules, this time named greeter and closer, but in the doorman directory. It’s also important to note that the declarations use the pub keyword. This exposes both the greeter and the closer modules as accessible outside of the doorman module.
  4. The files src/doorman/greeter.rs and src/doorman/closer.rs are found and loaded as their respective modules. Making both the greet() and close() functions accessible.
  5. Finally, from our main() function, we can greet the user with doorman::greeter::greet(name); and say goodbye to them with doorman::closer::close(name);

Final Thoughts

--

--

--

Just a programmer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build Your Own OS #3

⭐️ Go: The Complete Bootcamp Course

Two Hip Friends — JHipster and Neo4j

Tutorial: advanced use of watchdog in Python : excluding files — a git auto commit example (part…

NebulaStream 0.2 is available!

Getting started with AR development: Tools, software, and languages you should know

Temp Check: Halfway Through Software Engineering Bootcamp

The Challenges of Building Remote Patient Monitoring Systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Erik Tate

Erik Tate

Just a programmer

More from Medium

Docker buildx on Apple Silicon

Scrift — Part 1: Getting Started

Gomo — 📐 Simple Golang multi modules tool.

How to run own self-hosted MQTT broker?

Logs