Sub topic 6
What Cargo generates
This chapter assumes you have installed Rust with rustup and can run cargo --version in a terminal. It looks at the files and folders that cargo new creates and the ones that appear after your first build.
The files from cargo new
Run this command in a directory where you keep projects:
cargo new hello_cargo
cd hello_cargoCargo creates a directory named after the project. Inside it you get:
hello_cargo/
├── .git/
├── .gitignore
├── Cargo.toml
└── src/
└── main.rsEach entry has a specific job:
| Path | Purpose |
|---|---|
| `Cargo.toml` | The manifest. It holds the package name, version, edition, and dependencies. |
| `src/` | Holds your source code. Cargo expects code here, not in the project root. |
| `src/main.rs` | The crate root of a binary crate. It contains the `main` function. |
| `.gitignore` | Tells Git to skip build output. |
| `.git/` | A new Git repository, created for you. |
Cargo only creates the Git repository if the directory is not already inside one. To skip it, pass --vcs none.
The generated source file
The src/main.rs that Cargo writes is a complete program:
fn main() {
println!("Hello, world!");
}Cargo treats src/main.rs as the root of a binary crate by convention. You do not need to list it in Cargo.toml. If you create a library with cargo new --lib, Cargo generates src/lib.rs instead.
The manifest
Open Cargo.toml. It looks like this:
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
[dependencies]The [package] table describes the project. The edition value depends on your Cargo version, so yours may differ. The [dependencies] table starts empty. A later chapter covers editing it.
What appears after the first build
Run the project once:
cargo runTwo new items show up in the project directory:
target/holds everything Cargo produces. Debug builds go intarget/debug, and release builds go intarget/release.Cargo.lockrecords the exact version of every dependency that was used. Cargo writes and updates this file itself. Do not edit it by hand.
The .gitignore that Cargo generated lists /target, so build output stays out of version control. Cargo.lock is not ignored. For a binary project you should commit it, so that everyone who builds the program gets the same dependency versions.
Common mistakes
- Putting source files in the project root. Cargo looks in
src/. Amain.rsplaced next toCargo.tomlis ignored. - Running Cargo commands from the wrong directory. Cargo searches upward from your current directory for a
Cargo.toml. If it finds none, it reports an error that it could not find one. - Committing
target/. It is large and machine-specific. Check that your.gitignorestill lists it if you start from an existing project. - Editing
Cargo.lockmanually. ChangeCargo.tomland let Cargo update the lock file.
Inspecting the result
Check the project files yourself with a short command:
ls -AOn a fresh project after cargo run, you should see .git, .gitignore, Cargo.lock, Cargo.toml, src, and target. On Windows PowerShell, use Get-ChildItem -Force instead.
Exercise
You run cargo new demo and then cargo build. Which two new items appear in the project directory only after the build, and which of them should you normally leave out of version control?
Show answer
Cargo.lock and target/ appear after the build. Leave out target/, which the generated .gitignore already does. Keep Cargo.lock under version control for a binary project.
Exercise
A teammate puts a new file main.rs next to Cargo.toml and edits it, but cargo run still prints the old output. Why?
Show answer
Cargo builds the binary crate from src/main.rs. The file in the project root is not part of the crate, so edits to it have no effect. Move the code into src/main.rs.