Hello, Cargo
Set up the project, print something, and get comfortable with the build loop.
By the end of this part you’ll have a binary that runs. That’s it — no word counting yet. The point is to make the edit-build-run loop feel automatic before we add real logic.
What you need
- rustup — installs
rustc,cargo, and keeps them updated - Any editor. rust-analyzer is worth setting up early
Check your install:
cargo --version
Create the project
cargo new wordcount
cd wordcount
Cargo generates three things worth knowing:
| Path | What it is |
|---|---|
Cargo.toml |
Package name, version, dependencies |
src/main.rs |
The entry point of your binary |
Cargo.lock |
Exact dependency versions — commit this for binaries |
Run it
cargo run
You should see Hello, world!. Cargo compiled a debug build into target/debug/ and
executed it.
Make it yours
Open src/main.rs and replace it:
fn main() {
let name = "wordcount";
println!("{name} v{}", env!("CARGO_PKG_VERSION"));
}
Two things happened there:
println!is a macro, not a function — the!is how you tell. It formats at compile time, so a mismatched placeholder is a compile error, not a runtime surprise.env!("CARGO_PKG_VERSION")reads the version out ofCargo.tomlduring compilation.
Run it again:
cargo run
# wordcount v0.1.0
The loop you’ll use constantly
cargo check # type-check only — fastest feedback
cargo run # build + run
cargo build --release # optimized build, much slower to compile
Reach for cargo check while you’re iterating. It skips code generation, so it’s often
several times faster than a full build.
Before moving on
Try breaking it on purpose: change {name} to {nmae} and run cargo check. Read the
error. Rust’s compiler messages are the best teaching tool in the language — get used to
actually reading them instead of scanning for the line number.
Next up: reading arguments off the command line.