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.
- All episodes are in the Rust Tutorial for Beginners playlist.
- Versions used: Rust 1.99.1 on Ubuntu, VS Code with rust-analyzer.
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.
cargo --version3. New project
To create a new project, type cargo new, followed by the project name. Let's build a small dice roller.
cargo new dice_roller4. Open in VS Code
Move into the folder, and open it in VS Code.
cd dice_roller5. 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.
cargo run9. 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.
cargo build
./target/debug/dice_roller11. cargo check
While you're coding, cargo check is even faster. It checks for errors without building the program.
cargo check12. 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.
cargo add rand13. 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:
fn main() {
let roll = rand::random_range(1..=6);
println!("You rolled a {roll}!");
}15. Run it
Run it with cargo run.
cargo run16. Run again
Every time we run it, we get a new roll.
cargo run17. Release build
When your program is ready to share, build it with the release flag. Cargo turns on optimizations, so it runs much faster.
cargo build --release
./target/release/dice_rollerRecap
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.