Cargo
The Rust package manager for managing dependencies, building, and testing projects.
Project Creation
Create New Binary Project
Creates a new project with a binary application structure.
Parameters:
project_name: The name of the new project directory.
cargo new [project_name]
Create New Library Project
Creates a new project with a library structure.
cargo new --lib [project_name]
Initialize in Existing Directory
Initializes a new Cargo package in the current directory.
cargo init
Building and Running
Build Project (Debug)
Compiles the local package and all of its dependencies. Default is debug mode (unoptimized).
cargo build
Build Project (Release)
Compiles with optimizations turned on. Output goes to target/release.
cargo build --release
Run Project
Builds and executes the binary target.
cargo run
Run with Arguments
Pass arguments to the binary by separating them with --.
cargo run -- arg1 arg2
Check Code
Quickly checks the code for compilation errors without generating an executable. Faster than build.
cargo check
Dependencies
Add Dependency
Adds a dependency to Cargo.toml.
Parameters:
crate_name: The name of the crate to add.
cargo add [crate_name]
Add Development Dependency
Adds a dependency used only for development (testing, examples).
cargo add --dev [crate_name]
Add Build Dependency
Adds a dependency used only by build scripts (build.rs).
cargo add --build [crate_name]
Update Dependencies
Updates dependencies as recorded in the Cargo.lock file.
cargo update
Tree View of Dependencies
Displays a tree visualization of the dependency graph.
cargo tree
Testing and Documentation
Run Tests
Executes all unit and integration tests.
cargo test
Run Specific Test
Runs only tests containing the specified name.
cargo test [test_name]
Build Documentation
Builds documentation for the package and its dependencies.
cargo doc
Open Documentation
Builds and opens documentation in the browser.
cargo doc --open
Cleaning and Maintenance
Clean Build Artifacts
Removes the target directory.
cargo clean
Fix Common Issues
Automatically fixes some compiler warnings and issues.
cargo fix
Format Code
Formats all source files according to rustfmt code style.
cargo fmt
Lint Code
Runs clippy to catch common mistakes and improve your Rust code.
cargo clippy
Publishing
Login to Crates.io
Saves the API token to local credentials.
cargo login [api_token]
Publish Crate
Uploads the current package to crates.io.
cargo publish
Package without Publishing
Assembles the local package into a distributable tarball.
cargo package
Advanced
Install Binary from Crate
Installs a Rust binary crate locally.
cargo install [crate_name]
Search Crates
Search for packages on crates.io.
cargo search [query]
Expand Macros
Shows the result of macro expansion (requires cargo-expand).
cargo expand
Configuration & Environment
Common Environment Variables
CARGO_HOME: Location of the cargo home directory (default:~/.cargo).RUSTFLAGS: Flags to pass torustc.RUST_LOG: Log level for cargo and rust programs (e.g.debug,info).
Config File Location
Local configuration can be found in .cargo/config.toml in your project directory. Global configuration is in ~/.cargo/config.toml.