Starting a Rust project with Cargo
Summary
Cargo builds Rust code, downloads dependencies, and builds those libraries. A new package is cargo new, a Cargo.toml manifest, and source under src. Use cargo check while editing, cargo run to try the program, and cargo build --release for the optimized binary.
Create the package
Official installers include Cargo. cargo --version prints a version when it is present. cargo new hello_cargo creates that directory with Cargo.toml and src/main.rs, and starts a Git repo unless you are already in one. Pass --bin for a program and --lib for a library. --vcs none skips version control. cargo init writes a manifest for code you already have if you move it under src.
Cargo.toml is TOML. [package] holds the name, version, and edition. Both pages show version 0.1.0 and edition 2024 on a new package. [dependencies] lists other packages, called crates. The generated main prints a hello-world line. Source belongs in src. The top directory is for a README, license, and configuration, not for code.
The first cargo build also writes Cargo.lock, which pins exact dependency versions. Leave it for Cargo to edit. With no dependencies the file is sparse.
Build, check, run
cargo build writes a debug binary to target/debug/hello_cargo, or target\debug\hello_cargo.exe on Windows. Debug is the default: it compiles faster and runs slower because optimizations are off. cargo run builds if sources changed, then runs the binary. If nothing changed, it skips the compile. cargo check stops before producing an executable, so it is faster while you are still editing. Build for real when you need the file.
cargo build --release optimizes and writes target/release instead of target/debug. It compiles more slowly and runs faster. Benchmark that binary, not the debug one. The same three commands work on every operating system. For someone else's project, clone it, enter the directory, and run cargo build. Cargo pays off once there is more than one file or a dependency. Starting with it means adding a dependency later is the normal step.
Claims
- id: C1 claim: Cargo builds your code, downloads the libraries it depends on, and builds those libraries. source: S1 evidence: "Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries."
- id: C2 claim: cargo new creates a project directory plus a Cargo.toml file and a src directory that contains main.rs. source: S1 evidence: "Cargo has generated two files and one directory for us: a Cargo.toml file and a src directory with a main.rs file inside."
- id: C3 claim: cargo check confirms the project compiles without producing an executable, and it is often faster than cargo build for that reason. source: S1 evidence: "cargo check is much faster than cargo build because it skips the step of producing an executable."
- id: C4 claim: A release build uses optimizations and writes the binary under target/release rather than target/debug. source: S1 evidence: "This command will create an executable in target/release instead of target/debug ."
- id: C5 claim: cargo new starts a package; pass --bin for a program and --lib for a library. source: S2 evidence: "To start a new package with Cargo, use cargo new"
- id: C6 claim: Cargo.toml is the manifest and holds the metadata Cargo needs in order to compile the package. source: S2 evidence: "This is called a manifest , and it contains all of the metadata that Cargo needs to compile your package."
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project, Hello, Cargo!, MIT / Apache-2.0; The Rust Project, Creating a New Package, MIT / Apache-2.0.
Cite this
iinventedlight. 2026. "Starting a Rust project with Cargo." iinventedlight Library, iil:note:1b7gv6j2, version of 2026-11-03. https://iinventedlight.com/library/notes/rust-cargo-new-project/. License: CC-BY-4.0.
@misc{iil_note_1b7gv6j2,
author = {{iinventedlight}},
title = {{Starting a Rust project with Cargo}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-03},
url = {https://iinventedlight.com/library/notes/rust-cargo-new-project/},
note = {Permanent ID iil:note:1b7gv6j2. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/rust-cargo-new-project.md.