0Pricing
Learn Rust Coding · Lesson

Cargo Basics

Build and run.

Cargo Basics is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Cargo?

Cargo is Rust's build system and package manager. It compiles your code, downloads dependencies, runs tests, and builds documentation.

  • Comes bundled with the Rust toolchain
  • Manages a project's Cargo.toml manifest
  • Handles the whole build lifecycle for you

Creating a New Project

Use cargo new to scaffold a binary project. It creates a directory with a src/main.rs and a Cargo.toml.

  • cargo new my_app makes a binary crate
  • cargo new my_lib --lib makes a library crate
cargo new my_app
cd my_app

The Default main.rs

A fresh binary project starts with a tiny main.rs. The main function is the entry point of every Rust binary.

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

Building with cargo build

cargo build compiles your project into the target/debug directory. The first build also resolves and compiles dependencies.

  • Debug builds are unoptimized but fast to compile
  • The binary lands in target/debug/my_app
cargo build

Running with cargo run

cargo run builds (if needed) and then runs the binary in one step. It's the fastest way to iterate during development.

fn main() {
    let name = "Cargo";
    println!("Running with {name}!");
}

Checking with cargo check

cargo check analyzes your code for errors without producing a binary. It is much faster than a full build and ideal for tight edit-compile loops.

cargo check

Release Builds

Add --release to enable optimizations. Release builds are slower to compile but produce faster binaries placed in target/release.

cargo build --release
cargo run --release

Passing Arguments

Arguments after -- are passed to your program, not to Cargo. Read them with std::env::args.

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    println!("Got {} arguments", args.len());
    for arg in &args {
        println!("{arg}");
    }
}

Cleaning Artifacts

cargo clean deletes the target directory, removing all compiled output. Use it to force a fresh build or reclaim disk space.

cargo clean

The Cargo.lock File

Cargo records the exact resolved dependency versions in Cargo.lock.

  • For binaries: commit it for reproducible builds
  • For libraries: usually not committed
  • It is generated and updated automatically

Common Cargo Commands

A quick reference of the everyday commands:

  • cargo new — create a project
  • cargo build — compile
  • cargo run — build and run
  • cargo check — fast error check
  • cargo test — run tests
  • cargo doc — build docs

Quick Check

Which command compiles your code without producing an executable binary?

Recap

You learned the core Cargo workflow:

  • cargo new scaffolds projects
  • cargo build compiles, cargo run builds and runs
  • cargo check is the fast error check
  • --release enables optimizations
  • Cargo.lock pins dependency versions

Cargo is the backbone of every Rust project.

Frequently asked questions

Is the “Cargo Basics” lesson free?

Yes — the full text of “Cargo Basics” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Cargo Basics”?

Build and run. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cargo Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cargo Basics
  2. Cargo.toml
  3. Workspaces
  4. Features and Profiles
← Back to Learn Rust Coding