M Logo
๐Ÿ“ Rust for Beginners ยท Part 2 of 22

Rust Cargo Tutorial: cargo new, cargo build and cargo run Explained

November 10, 2026

Welcome to episode 2 of Rust for Beginners, a series for complete beginners (no programming experience needed). In the last video, we installed Rust and ran our first program with rustc. Today, we'll meet Cargo, Rust's build tool and package manager. Every code block below is the exact code from the video, ready to copy.

1. Why Cargo

Rustc is fine for one file, but real projects have many files and libraries. Cargo creates, builds, runs and manages all of that for you.

2. Check Cargo

Cargo was installed together with Rust. Let's check its version.

bash
cargo --version

3. New project

To create a new project, type cargo new, followed by the project name. Let's build a small dice roller.

bash
cargo new dice_roller

4. Open in VS Code

Move into the folder, and open it in VS Code.

bash
cd dice_roller

5. Project layout

Cargo created everything we need: a Cargo.toml file, a source folder with main.rs, and even a Git repository.

6. Cargo.toml

Cargo.toml describes the project: its name, version, Rust edition, and the libraries it depends on.

7. main.rs

And main.rs already contains a Hello World program.

8. cargo run

To compile and run it in one step, use cargo run.

bash
cargo run

9. Output

Cargo compiled the project and printed Hello, world.

10. cargo build

Cargo build only compiles. The program is saved inside the target, debug folder, and we can run it directly.

bash
cargo build
./target/debug/dice_roller

11. cargo check

While you're coding, cargo check is even faster. It checks for errors without building the program.

bash
cargo check

12. Add a crate

Now let's add a library. In Rust, libraries are called crates. We'll add the rand crate to make random numbers.

bash
cargo add rand

13. Dependency added

Cargo downloaded rand, and added it to Cargo.toml under dependencies.

14. Write the code

Let's replace the code. We ask rand for a random number from one to six, and print it.

src/main.rs:

rust
fn main() {
    let roll = rand::random_range(1..=6);
    println!("You rolled a {roll}!");
}

15. Run it

Run it with cargo run.

bash
cargo run

16. Run again

Every time we run it, we get a new roll.

bash
cargo run

17. Release build

When your program is ready to share, build it with the release flag. Cargo turns on optimizations, so it runs much faster.

bash
cargo build --release
./target/release/dice_roller

Recap

To recap: cargo new creates a project, cargo run builds and runs it, cargo check finds errors fast, cargo add adds crates, and cargo build release makes the final version.

ยฉ 2026 All Rights Reserved By Mahmudul Alam