Essential commands
Cargo commands on Windows
# 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
Windows-specific
Cargo configuration on Windows
Cargo reads configuration from %USERPROFILE%\.cargo\config.toml (global) or .cargo\config.toml in your project. Useful Windows settings:
[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
Slow builds?
Speed up Cargo builds on Windows
- Add Windows Defender exclusions for
~\.cargoand your project'stargetdirectory — the biggest single improvement - Use
cargo checkinstead ofcargo buildduring development — type-checks without linking - Install
cargo-nextestfor faster test execution:cargo install cargo-nextest - Use the
craneliftbackend on nightly for faster debug builds:-Zcodegen-backend=cranelift
FAQ
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.