Below is a comprehensive and detailed list of commands and techniques for cargo
, the Rust package manager and build system.
-
Initialize a new Cargo project:
cargo new project_name
-
Initialize a new Cargo project in an existing directory:
cargo init
-
Build a project:
cargo build
-
Build a project with optimizations (release mode):
cargo build --release
-
Run the project:
cargo run
-
Run tests:
cargo test
-
Check the code for errors without building:
cargo check
-
Update dependencies to the latest versions allowed by
Cargo.toml
:cargo update
-
Clean the target directory:
cargo clean
-
Package the project into a distributable package:
cargo package
-
Publish a package to crates.io:
cargo publish
-
List available subcommands and options:
cargo --help
-
Build and run with custom features:
cargo build --features "feature_name" cargo run --features "feature_name"
-
Cross-compile for a different target:
cargo build --target target_triple
-
Run a specific test:
cargo test test_name
-
Generate a new Rust binary crate:
cargo new binary_crate --bin
-
Generate a new Rust library crate:
cargo new library_crate --lib
-
Show detailed information about a package:
cargo show package_name
-
List all dependencies:
cargo tree
-
Check code for common errors (with
clippy
):cargo clippy
-
Format code according to style guidelines (with
rustfmt
):cargo fmt
-
Run a specific binary from a package:
cargo run --bin binary_name
-
Run a specific example from a package:
cargo run --example example_name
-
Build documentation:
cargo doc
-
Open the documentation in a web browser:
cargo doc --open
-
Create a new workspace:
cargo new workspace_name --vcs none
-
Add a new package to a workspace:
cargo new package_name --lib
-
Build all packages in a workspace:
cargo build --workspace
-
Run tests for all packages in a workspace:
cargo test --workspace
-
Clean all packages in a workspace:
cargo clean --workspace
-
Add a dependency to the
Cargo.toml
file:cargo add dependency_name
-
Remove a dependency from the
Cargo.toml
file:cargo rm dependency_name
-
Update all dependencies to the latest versions:
cargo update
-
List outdated dependencies:
cargo outdated
-
Show the current configuration:
cargo config --list
-
Set configuration options for Cargo:
cargo config --set key=value
-
Reset Cargo configuration to default:
cargo config --reset
-
Build for a different architecture or operating system:
cargo build --target target_triple
-
Specify a custom target directory:
cargo build --target-dir /path/to/dir
-
Profile a build with
perf
:cargo build --release perf record --call-graph dwarf cargo run perf report
-
Benchmark code using
criterion
:cargo add criterion
-
Write and run benchmarks:
#[bench] fn my_benchmark(b: &mut Bencher) { b.iter(|| { /* code to benchmark */ }); }
cargo bench
-
Define custom Cargo commands using plugins:
[package] name = "my_package" version = "0.1.0" [dependencies] cargo = "1.0"
-
Install and use Cargo plugins:
cargo install cargo-edit cargo install cargo-metadata
-
List installed Cargo plugins:
cargo install --list
This extensive list of cargo
commands and techniques covers a broad range of functionalities, from basic project management and build commands to advanced techniques like custom commands, dependency management, profiling, and working with workspaces. The commands provided can help you efficiently manage, build, and deploy Rust projects, handle various configurations, and perform advanced operations for development and profiling.