Lesson 2 / 25
Hello, World and cargo
One tool to build, run, test and publish.
cargo new, run, build, test
cargo is Rust's build tool and package manager. cargo new app creates a project with Cargo.toml and src/main.rs; cargo run compiles and runs; cargo build --release produces an optimised binary; cargo test runs tests; cargo add adds dependencies. println! is a macro (note the !) that formats text, with {name} inline arguments and format specifiers for width, alignment and precision.
A first program, run
I ran this with Rust 1.99.0 (cargo run, edition 2024, standard library only). println! inserts variables by name and formats width, alignment and decimal places.
fn main() {
let name = "Ferris";
let crates = 3;
println!("Hello, {name}! You have {crates} crates to review.");
println!("{:>8}|{:<6}|{:.2}", "right", "left", 3.14159);
}
Output:
Hello, Ferris! You have 3 crates to review. right|left |3.14
Use --release for benchmarks
Debug builds are much slower; always measure performance with cargo build --release or cargo run --release.
Quick check: Why does println! end with an exclamation mark?
- It is a macro, not a function
- It prints in bold
- It panics after printing
- It runs at compile time only
Answer
It is a macro, not a function — Macros generate code.