Rust Cargo Basics

Cargo is the official build tool and package manager for Rust. Picture a site manager at a construction project: Cargo orders the materials, reads the blueprint, builds the structure, and inspects the result. Every Rust installation includes Cargo, so no separate download is needed.

Why Cargo Matters

Small programs compile with a single compiler command. Real projects contain many files, outside libraries, and test code. Cargo handles all of that with short commands, so you spend your time writing code instead of managing build steps.

  • Creates new projects with a standard folder layout
  • Compiles your code and runs the finished program
  • Downloads libraries your project needs
  • Runs tests and builds documentation

Creating a Project

Open a terminal in any folder and type the command below. Cargo builds a ready-to-run project named garden.

cargo new garden
cd garden

The Folder Layout

Cargo produces a small, predictable structure. Every Rust developer recognizes it, which makes shared projects easy to read.

garden/
|-- Cargo.toml      (project settings and dependency list)
|-- Cargo.lock      (exact versions used, created after the first build)
|-- src/
|   |-- main.rs     (program entry point)
|-- target/         (compiled output, created after the first build)

The Cargo.toml File

The Cargo.toml file works like an ID card for your project. It lists the project name, the version, the Rust edition, and every library the project uses.

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

[dependencies]

The [package] section describes the project. The [dependencies] section stays empty until you add libraries.

Everyday Commands

Four commands cover most daily work. Run them from the folder that holds Cargo.toml.

CommandWhat It Does
cargo buildCompiles the project and stores the result in target/debug
cargo runCompiles when needed and starts the program
cargo checkVerifies the code compiles without producing a program
cargo build --releaseCompiles with full optimization into target/release

Debug and Release Builds

A debug build compiles quickly and includes extra information that helps find mistakes. A release build takes longer to compile, yet the finished program runs much faster. Use debug while you write code and release when you share the program with others.

 Source code (src/main.rs)
        |
        v
 +-----------------+
 |      Cargo      |
 +-----------------+
    |           |
    v           v
 debug build   release build
 (fast to      (fast to
  compile)      run)

Using cargo check

The compiler needs time to generate machine code. The cargo check command skips that step and only verifies that your code follows Rust rules. Developers run it often while typing, because it returns results within seconds.

A Complete First Session

The default src/main.rs already prints a greeting. Replace its content with a custom message and run it.

fn main() {
    println!("Welcome to the garden project!");
}

Type cargo run in the terminal. Cargo prints compile messages first, followed by the line your program produces.

$ cargo run
   Compiling garden v0.1.0
    Finished dev profile
     Running `target/debug/garden`
Welcome to the garden project!

Understanding Cargo.lock

Cargo creates Cargo.lock automatically. The file records the exact version of every library in use. Anyone who builds the project later receives identical versions, so the program behaves the same on every computer. Never edit this file by hand.

Cleaning Up

The target folder grows large over time. Run cargo clean to delete it. Cargo rebuilds the folder the next time you compile.

Common Beginner Mistakes

Running Cargo commands from the wrong folder causes the most confusion. Cargo searches the current folder for Cargo.toml and reports an error when the file is missing. Move into the project folder before typing commands.

Editing files inside target wastes effort, because Cargo overwrites them on every build. Change only files in src and the settings file.

Key Points

  • Cargo builds, runs, tests, and manages libraries for Rust projects.
  • cargo new creates a project with a standard layout.
  • Cargo.toml stores settings and dependencies.
  • cargo check gives the fastest feedback while coding.
  • Release builds run faster than debug builds.