Cargo guide

Cargo on Windows — build, dependencies & config guide

Cargo is Rust's built-in build system and package manager. On Windows, it works identically to Linux and macOS. Use cargo build, cargo run, cargo test and cargo add to manage your project and dependencies.

Cargo commands on Windows

PowerShell
# Create a new binary project:
PS> cargo new my-app
# Create a library:
PS> cargo new --lib my-lib
# Add a dependency:
PS> cargo add serde --features derive
# Build (debug):
PS> cargo build
# Build and run:
PS> cargo run
# Run tests:
PS> cargo test
# Check for errors without building:
PS> cargo check
# Install a binary crate:
PS> cargo install ripgrep

Cargo configuration on Windows

Cargo reads configuration from %USERPROFILE%\.cargo\config.toml (global) or .cargo\config.toml in your project. Useful Windows settings:

.cargo/config.toml
[build]
jobs = 8 # parallel compile jobs
[profile.dev]
opt-level = 1 # faster debug builds
[profile.release]
lto = true # smaller, faster binary
codegen-units = 1 # max optimisation

Speed up Cargo builds on Windows

  • Add Windows Defender exclusions for ~\.cargo and your project's target directory — the biggest single improvement
  • Use cargo check instead of cargo build during development — type-checks without linking
  • Install cargo-nextest for faster test execution: cargo install cargo-nextest
  • Use the cranelift backend on nightly for faster debug builds: -Zcodegen-backend=cranelift

Cargo questions

Where does cargo store downloaded packages on Windows?

Cargo stores downloaded crates in the global registry cache at %USERPROFILE%\.cargo\registry. Compiled artifacts go into the project's target directory. The global cache is shared across all projects so each crate version is only downloaded once.

cargo build is very slow on Windows

The most common cause is Windows Defender scanning build artifacts. Add %USERPROFILE%\.cargo and your project's target folder to Defender exclusions. Also check: is your project on a OneDrive-synced folder? Moving it to a non-synced location can halve build times.