[Avg. reading time: 7 minutes]

Cargo

Cargo is a Rust’s official build system and package manager.

Cargo handles

  • Dependency management
  • Building code
  • Running tests
  • Running benchmarks
  • Publishing libraries
  • Version resolution

Cargo understands

  • targets
  • platforms
  • architectures
  • features

Sample file structure

.
├── Cargo.lock
├── Cargo.toml
├── src/
│   ├── lib.rs
│   ├── main.rs
│   └── bin/
│       ├── named-executable.rs
│       ├── another-executable.rs
│       └── multi-file-executable/
│           ├── main.rs
│           └── some_module.rs
├── examples/
│   ├── simple.rs
│   └── multi-file-example/
│       ├── main.rs
│       └── ex_module.rs
└── tests/
    ├── some-integration-tests.rs
    └── multi-file-test/
        ├── main.rs
        └── test_module.rs
  • Cargo.toml and Cargo.lock are stored in the root of your package (package root).
  • Source code goes in the src directory.
  • The default library file is src/lib.rs.
  • The default executable file is src/main.rs.
  • Other executables can be placed in src/bin/.
  • Benchmarks go in the benches directory.
  • Examples go in the examples directory.
  • Integration tests go in the tests directory.

Key files in to remember

Cargo.toml is about describing your dependencies in a broad sense, and is managed by you.

toml : Tom’s Obvious Minimal Language

TOML is Open Source, and case sensitive.

TOML aims to be a minimal configuration file format that’s easy to read due to precise semantics. TOML should be easy to parse into data structures in various languages.

  • TOML is case-sensitive.
  • A TOML file must contain only UTF-8 encoded Unicode characters.
  • Whitespace means tab (0x09) or space (0x20).
  • Newline means LF (0x0A) or CRLF (0x0D0A).

Lock file

  • Cargo.lock contains exact information about your dependencies. It is maintained by Cargo and should not be manually edited.

Cargo Subcommands

cargo --help

cargo --version

Creates project under new sub folder

cargo new projectname

Creates project under existing folder

cargo init projectname

Compile the current package

cargo build

Run the current package

cargo run

Compile the current package for Production

* cargo build --release

Check the current package for dependency errors

cargo check

Fetch dependencies of a package from the network

cargo fetch

Execute unit and integration tests of a package

cargo test

Remove generated artifacts

cargo clean

Update dependencies as recorded in the local lock file

cargo update

Build package’s documentation

cargo doc

Format the code

cargo fmt

Refer more Cargo commands in Cargo Book

Cargo Book

#cargo #packagemanagerVer 2.0.7

Last change: 2026-01-25