Cargo basics: creating Rust projects and managing dependencies

Cargo is Rust’s build tool and package manager. It creates projects, downloads dependencies from crates.io, compiles, runs tests and builds documentation. It’s installed alongside the compiler when you install Rust with rustup; check it with cargo --version.
Creating a project
Section titled “Creating a project”cargo new hello_cargocd hello_cargocargo runcargo new creates:
Cargo.toml, the manifest with the package name, version, edition (2024 by default on current Cargo) and dependencies.src/main.rs, a “Hello, world!” program. Usecargo new --libfor a library withsrc/lib.rsinstead.- A Git repository and
.gitignore, unless the directory is already inside a repository.
cargo run compiles the project into target/debug/ and runs it.
Adding dependencies
Section titled “Adding dependencies”Libraries in Rust are called crates. Add them with cargo add, which edits Cargo.toml for you:
cargo add serde --features derivecargo add serde_jsonThat produces a [dependencies] section like this (cargo add writes the latest version number at the time you run it):
[dependencies]serde = { version = "1", features = ["derive"] }serde_json = "1"Now use them in src/main.rs:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]struct Config { name: String, retries: u32,}
fn main() -> Result<(), serde_json::Error> { let config = Config { name: "demo".to_string(), retries: 3 };
let json = serde_json::to_string(&config)?; println!("{json}");
let parsed: Config = serde_json::from_str(&json)?; println!("{parsed:?}"); Ok(())}cargo run downloads and builds the crates, then prints:
{"name":"demo","retries":3}Config { name: "demo", retries: 3 }Version requirements and Cargo.lock
Section titled “Version requirements and Cargo.lock”A version string in Cargo.toml is a requirement, not an exact pin. "1" and "1.0" both mean “any semver-compatible 1.x release”, the same as ^1.0. Use "=1.0.100" for an exact version or ">=1.2, <1.5" for a range.
The exact versions Cargo picked are recorded in Cargo.lock, so every build uses the same dependency tree until you change it. Commit Cargo.lock to version control.
To move to newer compatible versions:
cargo update # everything, within the ranges in Cargo.tomlcargo update -p serde # just one cratecargo update never crosses a major version. To go from 1.x to 2.x, change the requirement in Cargo.toml (or run cargo add again).
Commands you’ll use daily
Section titled “Commands you’ll use daily”| Command | What it does |
|---|---|
cargo check | Type-checks without producing a binary. Much faster than build, so use it while editing. |
cargo build | Debug build. Add --release for an optimised build in target/release/. |
cargo run | Build and run. Arguments after -- go to your program. |
cargo test | Run unit, integration and doc tests. |
cargo fmt | Format code with rustfmt. |
cargo clippy | Run the Clippy linter. |
cargo doc --open | Build HTML docs for your crate and its dependencies and open them. |
cargo tree | Show the dependency tree, useful for finding why a crate is pulled in. |
cargo publish | Upload a library to crates.io (needs an API token from cargo login). |
cargo help <command> shows the options for any of these.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)