← All notes

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

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

Permanent ID: iil:note:1b7gv6j2

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.},
}

Markdown

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.