Cargo Basics: Rust Package Manager

Cargo is Rust’s official package manager and build tool. It handles everything from compiling your code, managing dependencies, running tests, and creating distributable packages. You use Cargo to create new projects and run them easily with simple commands.

When I first started learning Rust, one of the best surprises was how smooth the experience was thanks to Cargo. If you are coming from JavaScript, Cargo is like a mix of npm, webpack, and tsc, but much more integrated and consistent.

In this post, I will show you how to create, build, and run Rust projects using Cargo. If you have only used rustc directly until now, Cargo is about to make your workflow much easier.

What is Cargo?

Cargo is the official command-line tool for managing Rust projects. It helps you:

  • Create a new project
  • Compile your code
  • Add and manage dependencies
  • Run unit tests
  • Build release binaries
  • Publish your project

It comes installed automatically when you install Rust.

Creating a New Project

To start a new Cargo project:

cargo new hello_rust

This creates a new folder with this structure:

hello_rust/
├── Cargo.toml
└── src/
    └── main.rs
  • Cargo.toml is your project’s metadata and dependency list
  • src/main.rs is your starting code file

To enter the folder:

cd hello_rust

Running Your Project

Once inside a project directory, run the app using:

cargo run

This does two things: compiles your code (if it has changed) and runs the compiled binary.

Output:

Compiling hello_rust v0.1.0
    Finished dev [unoptimized + debuginfo] target(s)
     Running `target/debug/hello_rust`
Hello, world!

Building Without Running

If you only want to compile:

cargo build

The output goes to the target/debug/ folder by default.

For production-ready builds:

cargo build --release

This builds an optimized binary in target/release/.

Understanding Cargo.toml

This file is like package.json in Node.js. It includes:

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

[dependencies]

You add external libraries under [dependencies], like:

regex = "1.10.3"

Then run:

cargo build

Cargo fetches the crate and compiles it.

Adding Dependencies

Use cargo add (with cargo-edit installed):

cargo add rand

This updates Cargo.toml automatically.

Running Tests

To run all tests in your project:

cargo test

This finds functions marked with #[test] and runs them.

Checking Code Without Building

Use cargo check for a fast syntax and type check:

cargo check

This does not create a binary, so it is faster than build.

Publishing a Package

When your crate is ready, publish it to crates.io:

1. Login with:

cargo login <your-api-key>

2. Then run:

cargo publish

You need a unique package name and complete metadata in Cargo.toml.

Useful Commands Summary

CommandWhat it Does
cargo newCreate a new project
cargo runBuild and run your app
cargo buildCompile the project
cargo build –releaseOptimized build for production
cargo checkQuickly check code for errors
cargo testRun unit tests
cargo add <crate>Add a dependency (with cargo-edit)

Summary

Cargo is the center of your Rust development workflow. It simplifies everything from compiling to testing to publishing. Whether you are building a tiny script or a full application, Cargo keeps your project organized and easy to manage.