Rust
December 2, 2026 7 min read

Rust Project Structure and Cargo in Depth

HS
HEMANT SINGH
Technical Writer & Educator

1. What is Cargo?

Cargo is Rust's official package manager and build system.

It helps developers create, build, run, test, and manage Rust projects.

With Cargo, you can:

  1. Create new Rust projects.
  2. Compile Rust source code.
  3. Run Rust applications.
  4. Add external libraries.
  5. Manage project dependencies.
  6. Run automated tests.
  7. Build optimized applications.
  8. Organize large projects.

Important Cargo Commands


Command

Purpose

cargo new

Create a new project

cargo init

Initialize Cargo in an existing directory

cargo build

Compile a project

cargo run

Compile and execute a project

cargo check

Check code without producing an executable

cargo test

Run tests

cargo add

Add a dependency

cargo update

Update dependencies within version requirements

cargo clean

Remove generated build files

cargo doc

Generate documentation

cargo tree

Display dependency relationships

2. Create a Rust Project

Open your terminal and execute:

cargo new my_project

Cargo creates a new project.

Now open it:

cd my_project
code .

Default Project Structure

my_project/
│
├── Cargo.toml
│
├── src/
│ └── main.rs
│
└── .gitignore

Let's understand these files and folders.


File / Folder

Purpose

Cargo.toml

Project configuration and dependencies

src/

Contains Rust source code

main.rs

Application entry point

.gitignore

Files Git should ignore

3. Understanding Cargo.toml

Cargo.toml is the main configuration file of a Rust project.

Example:

[package]
name = "my_project"
version = "0.1.0"
edition = "2024"

[dependencies]

Explanation

[package]

Contains project information.

name

Defines the package name.

version

Defines the current package version.

edition

Defines the Rust language edition used by the project.

[dependencies]

Contains external libraries required by the project.

For example:

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Now your project can use Serde and serde_json.

4. What is a Crate?

A crate is the basic unit of compilation and distribution in Rust.

There are two main types of crates:

1. Binary Crate

A binary crate produces an executable application.

Example:

fn main() {
println!("Hello Rust!");
}

The main.rs file is commonly used as the entry point of a binary crate.

2. Library Crate

A library crate provides reusable code that other programs can use.

It usually starts from:

src/lib.rs

Example:

pub fn greet() {
println!("Hello from library!");
}

Difference


Binary Crate

Library Crate

Produces an executable

Provides reusable code

Common entry point: main.rs

Common entry point: lib.rs

Used to run applications

Used by other crates

A single Cargo package can contain both a library crate and one or more binary crates.

5. What is a Package?

A package is a collection of one or more crates managed by Cargo.

A package contains a Cargo.toml file.

Example:

my_package/
│
├── Cargo.toml
│
└── src/
├── main.rs
└── lib.rs

This package contains:

  1. One binary crate.
  2. One library crate.

Remember:

  1. Package: A Cargo-managed project.
  2. Crate: A unit of compilation.
  3. Module: A way to organize code inside a crate.

6. Understanding Modules

Modules help organize Rust code into smaller, manageable sections.

Suppose you have a student management application.

Instead of writing everything in one file, you can organize code like this:

src/
│
├── main.rs
├── student.rs
└── fees.rs

student.rs

pub fn get_student() {
println!("Student information");
}

fees.rs

pub fn calculate_fees() {
println!("Calculating student fees");
}

main.rs

mod student;
mod fees;

fn main() {
student::get_student();
fees::calculate_fees();
}

Output:

Student information
Calculating student fees

Here:

  1. mod student; declares the student module.
  2. mod fees; declares the fees module.
  3. pub makes the functions accessible outside their defining module.
  4. student::get_student() calls a function using its module path.

7. Organize Modules into Folders

For larger applications, modules can be organized into folders.

Example:

src/
│
├── main.rs
│
├── student/
│ ├── mod.rs
│ └── profile.rs
│
└── fees/
├── mod.rs
└── calculation.rs

student/profile.rs

pub fn show_profile() {
println!("Student Profile");
}

student/mod.rs

pub mod profile;

fees/calculation.rs

pub fn calculate() {
println!("Fee Calculation");
}

fees/mod.rs

pub mod calculation;

main.rs

mod student;
mod fees;

fn main() {
student::profile::show_profile();

fees::calculation::calculate();
}

Output:

Student Profile
Fee Calculation

Note: Modern Rust projects can also use the folder-based module convention without mod.rs, by placing student.rs beside a student/ directory. Both approaches are supported.

8. What is a Dependency?

A dependency is an external library that your project uses.

For example, if you want to work with JSON, you can use the serde_json dependency.

Add it using:

cargo add serde_json

Cargo updates your Cargo.toml file.

Example:

[dependencies]
serde_json = "1"

Now use it in your Rust program:

use serde_json::json;

fn main() {
let student = json!({
"id": 1,
"name": "Rahul"
});

println!("{}", student);
}

9. What is Cargo.lock?

When you add dependencies, Cargo creates a file named:

Cargo.lock

It records the exact dependency versions selected for your project.

Why is this useful?

Suppose your project uses:

serde_json = "1"

This allows compatible releases within the specified version requirement.

But the exact version selected for your build is recorded in Cargo.lock.

This helps make builds more reproducible.

Important:

  1. Cargo.toml describes dependency requirements.
  2. Cargo.lock records the resolved dependency versions.

For application projects, you should generally commit Cargo.lock to version control.

10. Understanding Cargo Build

Cargo provides different ways to compile your application.

Development Build

cargo build

Creates a development build.

The executable is usually generated inside:

target/debug/

Run the Application

cargo run

Compiles and executes the application.

Release Build

cargo build --release

Creates an optimized build.

The executable is generated inside:

target/release/

Difference


Development Build

Release Build

cargo build

cargo build --release

Faster compilation

More optimization

Useful during development

Useful for production

Usually larger runtime overhead

Usually faster execution

11. What is Cargo Check?

The command:

cargo check

Checks your Rust code for compilation errors without producing a final executable.

It is generally faster than a full build.

Use it frequently while developing your application.

12. What is Cargo Tree?

When your project contains many dependencies, it can be difficult to understand which libraries are being used.

Run:

cargo tree

It displays the dependency tree.

Example:

my_project
├── serde
│ └── serde_derive
└── serde_json
├── serde
└── itoa

This helps you understand direct and indirect dependencies.

13. What are Cargo Features?

Some Rust libraries provide optional features.

For example:

cargo add tokio --features macros,rt-multi-thread

This enables selected Tokio features.

You can also define dependencies manually:

[dependencies]
tokio = {
version = "1",
features = ["macros", "rt-multi-thread"]
}

Features help you enable only the functionality your application needs.

14. What is a Cargo Workspace?

A workspace allows multiple related Rust packages to be managed together.

This is useful for larger projects.

Suppose you are building a school management system with separate packages for:

  1. Student management.
  2. Fee management.
  3. Attendance management.
  4. Main application.

You can organize them in a workspace.

Workspace Structure

school_system/
│
├── Cargo.toml
│
├── main_app/
│ ├── Cargo.toml
│ └── src/
│ └── main.rs
│
├── student/
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs
│
└── fees/
├── Cargo.toml
└── src/
└── lib.rs

Root Cargo.toml

[workspace]
members = [
"main_app",
"student",
"fees"
]

resolver = "3"

This tells Cargo that the three packages belong to one workspace.

Why Use a Workspace?

  1. Manage multiple related packages.
  2. Share a common build directory.
  3. Run tests across packages.
  4. Manage dependencies centrally when needed.
  5. Keep large applications organized.

15. Cargo Profiles

Cargo supports different build profiles.

The most common profiles are:


Profile

Purpose

dev

Development

release

Production

test

Testing

bench

Benchmarking

You can customize profiles in Cargo.toml.

Example:

[profile.release]
opt-level = 3

opt-level controls optimization level. Higher optimization can improve runtime performance but may increase compilation time.

16. Useful Cargo Commands


Command

Description

cargo new app

Create a new application

cargo init

Initialize a project in the current directory

cargo build

Compile the project

cargo run

Compile and run

cargo check

Check compilation errors

cargo test

Run tests

cargo add serde

Add a dependency

cargo remove serde

Remove a dependency

cargo update

Update dependency resolution

cargo tree

Display dependencies

cargo clean

Remove build artifacts

cargo doc --open

Generate and open documentation

cargo build --release

Build an optimized application

17. Mini Project: Student Management Package

Let's organize a small student management application using modules.

Project Structure

student_app/
│
├── Cargo.toml
│
└── src/
├── main.rs
├── student.rs
└── fees.rs

student.rs

pub struct Student {
pub id: u32,
pub name: String,
}

pub fn show_student(student: &Student) {
println!("Student ID: {}", student.id);
println!("Student Name: {}", student.name);
}

fees.rs

pub fn calculate_fees(monthly_fee: f64, months: u32) -> f64 {
monthly_fee * months as f64
}

main.rs

mod student;
mod fees;

use student::Student;

fn main() {

let student = Student {
id: 101,
name: String::from("Rahul"),
};

student::show_student(&student);

let total_fees = fees::calculate_fees(500.0, 12);

println!("Total Fees: {}", total_fees);
}

Output

Student ID: 101
Student Name: Rahul
Total Fees: 6000

This example demonstrates how to separate application responsibilities into different modules.

18. Practice Exercises

  1. Create a new Cargo project.
  2. Understand the Cargo.toml file.
  3. Add the serde_json dependency.
  4. Create a separate module named student.
  5. Create another module named fees.
  6. Call functions from both modules in main.rs.
  7. Run cargo check.
  8. Run cargo build --release.
  9. Execute cargo tree.
  10. Create a workspace containing two small packages.