Today we are going to talk about the Rust project structure and how to organize your code. Although cargo workspace also enters in this equation we’ll not talk about it, for the sake of simplicity.

Introduction

When we write small programs it’s simple to keep tracking of the entire functionalities inside our head. To make simple calculus or exercise some algorithms you can do everything with just one single source code file, the problem is when you need to add even more functionalities to the scope.

In normal software projects, you need to write a lot more. The amount of source code will increase drastically and will be impossible to keep track of everything inside your head. Because of that you need to organize everything in a meaningful way.

This organization must encapsulate the implementations to show the compiler and programmers where everything is and how it is organized.

Rust code management

Rust’s way to solve this problem is to create 4 hierarchical structures in a Modular System(https://en.wikipedia.org/wiki/Modular_programming):

  1. Packages: A Cargo feature that lets you build, test and, share Crates. It could be considered the project.
  2. Crates: It’s a tree of modules that produces executables or libraries
  3. Module and use: It’s the responsible for the scope of your source code, lets you organize the privacy of your functionalities
  4. Paths: it’s a naming system, you use it for struct, functions, or modules.

Let’s see each one of them.

#rust

Rust Adventures: Rust projects management, understanding packages, Crates and modules
1.35 GEEK