MARY.WIN

RUSTY — BEGINNER — B01

Cargo and Composer: your first Rust
project

Install Rust, make a package, and learn which Cargo file does each Composer file's job.
Lesson 1 of 9 in Rust beginner track. Before you start: nothing; this is the first lesson.

You clone a Rust project from a colleague. At the top of the folder there's a Cargo.toml, next to it a src/ folder, and no vendor/ anywhere. Out of habit, you type composer install. Composer could not find a composer.json file.

Composer is right. There isn't one. A Rust project has its own manifest, its own lockfile and its own everyday commands, and each of them does a job you already know from Composer. By the end of this lesson you'll have installed Rust, made a package, and run this program.

This lesson covers four things: installing Rust with rustup, creating a package with cargo new, the four Cargo commands you'll type on most days, and reading a first program. For each one you get the command, the file it touches, and the Composer job it matches. It leaves out adding a crate, and where a crate's code lives instead of vendor/; that's Lesson 8. It assumes you've used Composer and know nothing about Rust yet. You don't have to memorize any flags. You can come back here whenever a command slips your mind.

Take a look at Example 1.

Example 1. The first program (examples/src/bin/b01_hello.rs)

// Lesson b01: the smallest program Cargo can build and run.
fn main() {
    println!("Hello from Cargo");
    println!("2 + 3 = {}", 2 + 3);
}

Once Rust is installed, run it from the examples folder of the course code.

TERMINAL
cargo run --quiet --bin b01_hello
Hello from Cargo
2 + 3 = 5

We'll come back to the exclamation mark in println! at the end.

Install the toolchain

Before you could run Composer, you needed PHP on the machine, and some way of keeping it current. Rust puts that job in one tool. rustup is a toolchain manager: it installs the compiler, rustc, and Cargo, and it updates both.¹ On Linux and macOS, the Rust Book's Installation page gives a one-line command that runs a script from sh.rustup.rs; copy it from there. On Windows, the Book sends you to the installer on rust-lang.org.

Then ask both tools for their versions:

TERMINAL
rustc --version
cargo --version

This course was checked with this toolchain:

OUTPUT
rustc 1.100.0 (0ef1bc386 2026-11-09)
cargo 1.100.0 (3acc79c0c 2026-11-05)

Later, rustup update moves you to the newest stable release.

Is rustup the compiler? No. rustc is the compiler. rustup manages which rustc you get, and Cargo calls rustc for you, which is why the rest of this lesson is about Cargo.

What cargo new writes

In PHP, composer init writes a composer.json. In Rust, cargo new palette writes a manifest, a source file and the folder that holds them. This is the recorded run, which added --vcs none for a reason in note ²:

TERMINAL
cargo new --vcs none palette
    Creating binary (application) `palette` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Take a look at Example 2, the manifest it wrote.

Example 2. The manifest cargo new wrote (palette/Cargo.toml)

[package]
name = "palette"
version = "0.1.0"
edition = "2024"

[dependencies]

There are two sections. [package] gives the name, the version and the edition, which picks the set of language rules the package is compiled under; this course uses edition 2024. [dependencies] is empty.

The other file is palette/src/main.rs. Its main() function is where the program starts. The nearest Laravel file is public/index.php, where every request enters, with one difference: main() runs once, and then the program exits.

Four commands for most days

While you edit, the question you ask most often is whether the code compiles, and you don't need a finished program to get the answer. Thankfully, Cargo has a command for exactly that question. cargo check asks whether the code compiles and stops there. It produces no program, so it's the fast one while you edit.

Three more commands cover most days:

  • cargo build writes the program under target/debug.
  • cargo run builds if it needs to, then runs the program. In the recorded palette run, cargo run --quiet printed Hello, world!.
  • cargo test builds and runs the tests. In PHP, that's PHPUnit's job.

You can also answer the compile question with cargo build, and it is, depending on how large the package has grown and how often you save and whether you need to run the result at all, either exactly the right command or a slower way of asking what cargo check asks, and only one of the two leaves a program in target/debug.

One flag finishes the set. cargo build --release builds with optimizations, into target/release. When you time a program, time that one. The last line of cargo check's recorded output and the last line of cargo build --release's show the two settings side by side:

OUTPUT
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
    Finished `release` profile [optimized] target(s) in 0.30s

Where did Cargo.lock come from?

The manifest in Example 2 held the view that it described the whole package. It was six lines long. It named the package palette, gave it version 0.1.0 and edition 2024, and kept an empty [dependencies] table the way a new pantry keeps a labelled shelf before there is anything to put on it. It had not been told about any other file. Then Cargo built the package, and a file named Cargo.lock appeared beside it.

That file is the lockfile, and you already know its PHP twin, composer.lock. Cargo.lock records the exact version of every dependency Cargo chose, so the next build uses the same ones. Cargo writes it, not you. The folder target/ arrives with the first build too; it's build output, and it stays out of Git.

What is the exclamation mark doing?

Go back to Example 1. Its main() has two lines. The first prints Hello from Cargo. The second prints 2 + 3 = 5: the {} inside the text is a placeholder, and 2 + 3 fills it.

println! ends in an exclamation mark, and that mark is how you tell a macro from a function. A macro writes more code while the program compiles, so by the time the program runs, it's ordinary Rust code. For now, read the ! as "macro" and move on.

If you'd like to see the PHP side, Example 3 is the same two lines.

Example 3. The PHP comparison (php/b01_hello.php)

<?php
// Lesson b01: the PHP side of "run a tiny program".
echo "Hello from PHP", PHP_EOL;
echo "2 + 3 = ", 2 + 3, PHP_EOL;

Run it with php php/b01_hello.php and it prints the PHP version of the same output:

OUTPUT
Hello from PHP
2 + 3 = 5

So the package builds, the program prints, and every file in the folder has one job you can name. I like that; it makes a new project easy to read. But ask the question that tidy picture lets you skip: which of these files belong in your repository? target/ doesn't. Cargo.lock is the interesting one. The Cargo Book's advice is to commit it when in doubt, which is advice for the moment before you've decided. Who else will build this package? Should they get exactly the versions your build chose? Is that what you wanted from composer.lock, too?

Testing

Two checks tell you whether this lesson stuck. First, predict before you answer: which command checks the code without producing a program, cargo check or cargo build? It's cargo check. cargo build produces the program, in target/debug.

Second, find any Rust project. Before you open anything, name the job of Cargo.toml, Cargo.lock and target/. Then open them and see. You can get these wrong a few times. The commands are short, and the compiler is patient.

TL;DR

rustup installs and updates the compiler, rustc, and Cargo. cargo new writes a package: Cargo.toml, the manifest, with [package] and an empty [dependencies], and src/main.rs, whose main() is where the program starts. cargo check asks whether the code compiles, cargo build writes the program to target/debug, cargo run builds and runs it, cargo test runs the tests, and cargo build --release builds with optimizations into target/release. The first build writes Cargo.lock, which records exact versions, and target/, which stays out of Git. println! is a macro. Next: the compiler refuses something PHP lets through.

¹ The rustc and cargo commands that rustup installs are proxies: each one hands the work to whichever toolchain is active. That's how rustup decides which rustc you get.

² cargo new starts a Git repository in the new folder unless the folder is already inside one or --vcs says otherwise. The recorded run passed --vcs none, so it created no repository.

The sources.

Programs, their output and the compiler's messages were recorded on rustc 1.100.0 on 2026-11-30. Book locators are to The Rust Programming Language (3rd edition), Programming Rust (3rd edition) and Learn Rust in a Month of Lunches.

SOURCES — 7
  • Rust Book ch. 1.1 "Installation", ch. 1.3 "Hello, Cargo!" and ch. 14.1 "Customizing builds with release profiles" [S20]
  • TRPL 3rd ed. ch. 1, Hello, Cargo! > Creating a Project with Cargo, Building and Running a Cargo Project, Building for Release 11.6; ch. 1, Hello, World! > Anatomy of a Rust Program 11.5; ch. 11, How to Write Tests; Preface (edition 2024) [S17]
  • Programming Rust 3rd ed. ch. 8, Specifying Dependencies > Cargo.lock [S18]
  • rustup book "Concepts" [S21]
  • Cargo Book cargo check, cargo new, "Cargo.toml vs Cargo.lock" [S22]
  • Composer docs "Basic usage" and "Command-line interface" [S28]
  • Laravel docs 13.x lifecycle.md (public/index.php entry point) [S3].

Rust Book: Installation · rustup book: Concepts · Rust Book: Hello, Cargo! · Cargo Book: Cargo.toml vs Cargo.lock · Cargo Book: cargo check · Rust Book: release profiles · Cargo Book: cargo new · Composer: basic usage · Composer: command-line interface · Laravel docs: request lifecycle

NOT CHECKED — 5
  • The Composer commands are comparisons and were not part of this course's checks; Composer's reply in the opening scene was seen in one separate run.
  • The install command is not reproduced here; it is on the Rust Book's Installation page.
  • Cargo.lock was observed after the package had been checked, run, tested and built; which of those commands wrote it first was not recorded.
  • That a macro writes more code while the program compiles follows The Rust Programming Language; this page does not show the generated code.
  • Book locators were checked against The Rust Programming Language and Programming Rust on 29 November 2026 and not re-read for this page.