It’s Different

When beginning with Rust and having the need to expand to other files in subfolders of the project, one can stumble over the module concept of Rust very fast.

Whatever technology or programming language we use, we tend to structure our ideas and concepts in directories and files, in order to maintain a valuable overview as the project accelerates and expands, to encapsulate knowledge, and to create strong coherence.

Creating these folders in a Rust project, it strikes home fast: Things are very different here. A C-based language programmer tends to import files to get the respective types or namespaces within one’s scope. Playing around with some context help of the IDE or searching the internet, we learn that Rust thinks in modules, not files.

Rust works with modules, as part of a module tree. There is no such thing as file imports.

A Module in Rust

Important concepts in the Rust Module System¹ are packagescratesmodules, and paths. This article focuses on modules² and paths, when defined in multiple files and how to bring the split parts together.

Creating a base project

Creating a new project from a template in one’s favorite IDE (or terminal editor) is usually the first step into a new project, and the Rust project will probably look similar to this:

project
|-- src
|-- target 
|-- .gitignore
|-- Cargo.lock
|-- Cargo.toml

#devops #software-development #programming #rust #rustlang

Explaining Rust’s Modules
1.10 GEEK