Rust Lesson 3 – Understanding Cargo Projects | Dataplexa

Understanding Cargo Projects

In this lesson, you will learn how Rust projects are structured when created using Cargo. Understanding this structure is essential for writing, organizing, and maintaining Rust applications.

Cargo enforces a standard project layout that makes Rust projects easy to understand, share, and scale.


What Is a Cargo Project?

A Cargo project is a Rust project managed by Cargo. It includes source code, configuration files, and optional dependencies.

Cargo automatically handles:

  • Project structure
  • Compilation
  • Dependency management
  • Testing and execution

This standardized approach keeps Rust projects clean and consistent.


Creating a Cargo Project

You already learned how to create a Cargo project using the following command:

cargo new my_project

This command creates a new directory with all required project files.


Default Cargo Project Structure

A basic Cargo project contains the following files and folders:

  • Cargo.toml
  • src/ directory
  • src/main.rs

Each part of this structure has a specific purpose.


The Cargo.toml File

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

It contains:

  • Project name
  • Version information
  • Rust edition
  • Dependencies

A typical Cargo.toml file looks like this:

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

[dependencies]

This file tells Cargo how to build and manage your project.


The src Directory

The src directory contains all Rust source code.

For binary projects (programs), the main file is:

  • src/main.rs

Cargo automatically looks for this file when compiling and running your program.


The main.rs File

The main.rs file is the entry point of a Rust program.

It must contain a main function:

fn main() {
    println!("Hello, world!");
}

Execution of the program always starts from the main function.


Building a Cargo Project

To compile a Rust project without running it, use:

cargo build

Cargo compiles the project and stores the output in the target directory.


The target Directory

The target directory is automatically created by Cargo.

It contains:

  • Compiled binaries
  • Intermediate build files
  • Debug and release outputs

You usually do not modify files inside this directory manually.


Running a Cargo Project

To compile and run a Rust project in one step, use:

cargo run

Cargo checks for changes, recompiles if needed, and executes the program.


Checking Code Without Building

Rust provides a fast way to check code for errors without producing a binary.

Use the following command:

cargo check

This is useful during development for quick feedback.


Adding Dependencies

Dependencies are external libraries used in your project.

They are added under the [dependencies] section in Cargo.toml.

Example:

[dependencies]
serde = "1.0"

Cargo automatically downloads and manages dependencies.


📝 Practice Exercises


Exercise 1

Create a new Cargo project named cargo_demo.

Exercise 2

Open the Cargo.toml file and identify the project name and version.

Exercise 3

Build the project without running it.


✅ Practice Answers


Answer 1

cargo new cargo_demo

Answer 2

The project name and version are defined under the [package] section.

Answer 3

cargo build

What’s Next?

Now that you understand Cargo projects and structure, you are ready to learn Rust’s basic syntax.

In the next lesson, you will write simple Rust statements and understand how Rust code is structured.