Course › Week 1 · Basics and ownership › Day 1 · lesson 1 of 42 · 5 min reading + 60–90 min practice
Cargo and your first program
Create, run and build a project, and read Cargo.toml the way you read composer.json.
On this page
Today is about the tool you will type most: cargo. It creates projects, downloads packages, compiles, runs, tests, formats and lints. In PHP terms it is Composer, Artisan and PHPUnit in one command.
By the end you will have created a project, read every file in it, run it, and seen one compiler error. Then the Book’s chapters 1–2 and the Rustlings intro.
Create a project
1. cargo new
cargo new makes a folder with a ready-to-run program in it, and a Git repository.
cargo new hello
cd hello
Creating binary (application) `hello` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html2. What it made
hello/
├── Cargo.toml (the project file, like composer.json)
├── .gitignore (ignores /target, where builds go)
└── src/
└── main.rs (the program)
That is the whole project. No framework, no config folder. Rust looks for src/main.rs by convention, the way Laravel looks for routes/web.php.
Cargo.toml
The project file. It is written in TOML, a format made of [sections] and key = value lines.
[package]
name = "hello"
version = "0.1.0"
edition = "2024"
[dependencies]
name and version are your package’s, like name and version in composer.json. [dependencies] is your require section, empty for now.
edition is new. An edition is a set of language rules, picked per project. New editions arrive every three years (2015, 2018, 2021, 2024) and an old project keeps compiling on a new compiler because it keeps its edition. Always use the one cargo new gives you.
The program
3. main.rs
Every Rust program starts in a function called main. fn declares a function; the body sits between braces.
fn main() {
println!("Hello, world!");
}
Hello, world!println! prints a line. The ! means it is a macro, not a function: it is expanded into code at compile time. For now, read ! as “this is a macro” and move on; you will use a handful of them (println!, vec!, format!) all the time.
Statements end with a semicolon, like PHP.
4. cargo run compiles, then runs
Here is what cargo run prints the first time, before your program’s output:
cargo run
Compiling hello v0.1.0 (/home/you/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/hello`
Hello, world!PHP reads your source every time it runs. Rust compiles it once into a single executable file, target/debug/hello, and then runs that file. You can copy it to another machine with the same operating system and run it there with no Rust installed. That is why Rust works so well for tools you hand to a Laravel app later in this course.
5. Printing values
{} in the string is a placeholder. You can name a variable inside the braces, or pass values after the string.
fn main() {
let app = "Nordic Stays";
let bookings = 1250;
println!("{app} has {bookings} bookings");
println!("{} has {} bookings", app, bookings);
}
Nordic Stays has 1250 bookings
Nordic Stays has 1250 bookingslet creates a variable. There is no $. Day 2 is all about variables; today, just print them.
Your first compiler error
A typo in a variable name. In PHP this prints a warning at runtime and carries on with null. Rust refuses to build:
fn main() {
let bookings = 1250;
println!("{} bookings", booking);
}
error[E0425]: cannot find value `booking` in this scope
--> src/main.rs:4:29
|
4 | println!("{} bookings", booking);
| ^^^^^^^ not found in this scope
|
help: a local variable with a similar name exists
|
4 | println!("{} bookings", bookings);
| +
For more information about this error, try `rustc --explain E0425`.
error: could not compile `hello` (bin "hello") due to 1 previous errorRead it from the top. error[E0425] is the error code. --> points at the file, line and column. The ^^^^^^^ underlines the exact spot, and help: suggests the fix.
Any error code can be explained in full with rustc --explain E0425. Use it whenever an error surprises you.
The other cargo commands you need
| Command | What it does | When |
|---|---|---|
cargo check | Checks that the code compiles, without building the executable. Much faster. | Constantly, while writing |
cargo run | Builds a debug version and runs it. | To try your program |
cargo build --release | Builds an optimised version in target/release/. Slower to compile, much faster to run. | Before measuring speed or shipping |
cargo fmt | Formats every file in the one standard Rust style. | Before every commit |
cargo clippy | Lints: points out code that works but is wrong, slow or unusual. | Before every commit |
cargo add <crate> | Adds a package to [dependencies]. | When you need a library |
Packages are called crates, and they live on crates.io, Rust’s Packagist. You will add your first one in Week 2.
Coming from PHP
Most of what you know from Composer has a direct cargo equivalent.
| PHP | Rust |
|---|---|
composer.json | Cargo.toml |
composer.lock | Cargo.lock (created on the first build) |
vendor/ | downloaded to ~/.cargo, compiled into target/ |
composer require guzzlehttp/guzzle | cargo add reqwest |
| Packagist | crates.io |
php artisan serve / php script.php | cargo run |
| PHP-CS-Fixer / Pint | cargo fmt |
| PHPStan / Larastan | cargo clippy, plus the compiler itself |
The biggest difference is the compiler. PHPStan is optional and runs after you write the code; in Rust, the checks are the language. Code with a type error does not exist as a program.
PracticeThe Book ch. 1–2, Rustlings intro, one small program
Read chapter 1 and chapter 2 of the Book. Chapter 2 builds a guessing game and uses several things you will only learn properly this week; type it anyway, the point is to feel the workflow.
In Rustlings: exercises/00_intro (intro1, intro2).
Exercise: a booking summary
Create a project called summary that prints three lines: a property name, the number of nights, and the guest count, each from its own variable. Then break it on purpose: misspell a variable, remove a quote, remove a brace. Read each error before you fix it.
Show the solution
fn main() {
let property = "Nyhavn flat";
let nights = 3;
let guests = 2;
println!("Property: {property}");
println!("Nights: {nights}");
println!("Guests: {guests}");
}
Property: Nyhavn flat
Nights: 3
Guests: 2Remember
cargo checkwhile writing,cargo runto try,cargo build --releaseto measure or ship.- Read a compiler error top to bottom: the code, the arrow to the line, the underline, then the
help:.
Tomorrow: variables are immutable unless you say otherwise, and every value has exactly one type.