C.W.K.
Stream
Lesson 03 of 06 · published

Your First Cargo Project

~10 min · foundations, cargo, hello-world

Level 0Rust Curious
0 XP0/80 lessons0/19 achievements
0/100 XP to next level100 XP to go0% complete

Enough theory. Let's make Rust do something. The entire create-build-run loop is three cargo commands.

cargo new

cargo new hello-rust scaffolds a complete project: a Cargo.toml manifest, a src/main.rs with a working hello-world, and a git repo. You don't assemble a Rust project by hand — cargo does it.

The shape of a project

Cargo.toml is the manifest (name, version, edition, dependencies). src/main.rs is the entry point — its fn main() is where execution starts. Source lives under src/; build output lands in target/ (which you gitignore).

main and println!

Every executable has exactly one fn main(). println! prints a line — and that ! matters: it marks a macro, not a function. Macros can do things functions can't, like checking your format string at compile time. Type a wrong placeholder and it won't compile. (Macros get a whole track later.)

cargo run = build + run in one step. Use cargo run while developing, cargo build --release for an optimized binary, and cargo check when you just want to know "does it compile?" without waiting for a full build.

The loop you'll live in

Edit src/main.rs, run cargo run, read the output (or the compiler error), repeat. That's the heartbeat of Rust development. Everything else — tests, dependencies, docs — hangs off the same cargo front door.

Code

Create and run your first project·bash
cargo new hello-rust
cd hello-rust

cargo run
#   Compiling hello-rust v0.1.0 (/path/to/hello-rust)
#    Finished dev [unoptimized + debuginfo] target(s)
#     Running `target/debug/hello-rust`
# Hello, world!
src/main.rs — edit it and re-run·rust
fn main() {
    let name = "Rustacean";
    // {name} interpolates directly; the ! marks println as a macro
    println!("Hello, {name}!");
    println!("2 + 2 = {}", 2 + 2);
}

External links

Exercise

Run cargo new for a project, then edit fn main to print your name and today's date as two separate lines. Then break it on purpose: remove the ! from println and run it. Read the error — what does the compiler suggest?
Hint
Without the !, the compiler looks for a function named println and can't find one. Notice how it still tries to help — that's the mentor showing up early.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue
💛 by Ttoriwarm

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.