Skip to content

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.

Terminal window
cargo new hello_cargo
cd hello_cargo
cargo run

cargo 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. Use cargo new --lib for a library with src/lib.rs instead.
  • A Git repository and .gitignore, unless the directory is already inside a repository.

cargo run compiles the project into target/debug/ and runs it.

Libraries in Rust are called crates. Add them with cargo add, which edits Cargo.toml for you:

Terminal window
cargo add serde --features derive
cargo add serde_json

That 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 }

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:

Terminal window
cargo update # everything, within the ranges in Cargo.toml
cargo update -p serde # just one crate

cargo 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).

CommandWhat it does
cargo checkType-checks without producing a binary. Much faster than build, so use it while editing.
cargo buildDebug build. Add --release for an optimised build in target/release/.
cargo runBuild and run. Arguments after -- go to your program.
cargo testRun unit, integration and doc tests.
cargo fmtFormat code with rustfmt.
cargo clippyRun the Clippy linter.
cargo doc --openBuild HTML docs for your crate and its dependencies and open them.
cargo treeShow the dependency tree, useful for finding why a crate is pulled in.
cargo publishUpload a library to crates.io (needs an API token from cargo login).

cargo help <command> shows the options for any of these.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)