lesson 1 of N · rust fluency
hello, cargo
rust is already installed on this box (rustc 1.99.1, cargo 1.99.1). this lesson gets you compiling real code and introduces cargo, the tool you will type more than any other.
why cargo exists
in python you juggle pip, venv, requirements.txt, and a build step you pretend not to need. in rust, one tool does the package managing, building, testing, and publishing. it is the package manager and the build system at once. every project is a crate.
the three commands that cover 90% of daily work
cargo new hello # scaffold a new project
cargo run # compile and run it
cargo test # run the tests
two others you will reach for constantly:
cargo check # fast compile check, no binary output
cargo build --release # optimized build for real deployment
cargo check is the one to lean on while learning. it runs the compiler's full analysis but skips code generation, so it is much faster than a full build. the compiler is your teacher in rust. run it early and often.
anatomy of the hello world crate
cargo new hello gives you:
hello/
├── Cargo.toml # manifest: name, version, dependencies
└── src/
└── main.rs # entry point for a binary crate
and main.rs starts life as:
fn main() {
println!("hello, world!");
}
four things worth noticing:
fndeclares a function.mainis the entry point, same as C.println!ends with!because it is a macro, not a function. it expands into real code at compile time. you will meet more of these.- strings use double quotes. single quotes are for single characters.
- there is no garbage collector and no runtime hiding behind this. what you write is roughly what runs.
do it now
cargo new helloin a scratch directorycd hello && cargo run- change the string, run
cargo check, thencargo runagain - deliberately break it: delete the semicolon after
println!(...). read the error. rust errors name the file, the line, and usually the fix. reading them is a core skill, not an annoyance.
check yourself
primary source
read chapter 1.3 of the Rust Book, hello cargo. it is short and covers exactly this ground with more detail on Cargo.toml.
next
lesson 2 covers variables and mutability: why rust makes you write mut at all, and what let actually does. cargo cheatsheet if you want the commands on one page.
stuck on anything here, or the quiz felt too easy or too hard? ask. that is literally what i am for.