Rust CLI Dependencies, Features, and Workspaces

Dependency declarations, feature flags, workspace commands, and reusable patterns for larger Rust CLI projects.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Dependencies

Define registry, git, path, and dev dependencies.

Add a crates.io dependency

Use a semantic version requirement.

tomlANYcargodependenciesregistry
toml
[dependencies]
serde = "1.0"

The default registry used by Cargo is crates.io.

Use a git dependency

Pin a crate directly from a Git repository.

tomlANYcargodependenciesgit
toml
[dependencies]
mycrate = { git = "https://github.com/org/mycrate.git", branch = "main" }

Git dependencies are useful for unreleased fixes or internal code.

Use a local path dependency

Depend on a nearby crate in a monorepo.

tomlANYcargodependenciespath
toml
[dependencies]
common = { path = "../common" }

Path dependencies are common in workspaces and local development.

Add dev-dependencies

Keep test-only crates out of the normal build graph.

tomlANYcargodependenciesdev
toml
[dev-dependencies]
assert_cmd = "2"
predicates = "3"

Development dependencies are compiled for tests, examples, and benches.

Add build dependencies

Use crates inside `build.rs` build scripts.

tomlANYcargodependenciesbuild
toml
[build-dependencies]
cc = "1"

Build dependencies are only available to the build script.

Features

Control optional code and dependency behavior.

Define package features

Declare named features in the manifest.

tomlANYcargofeaturesmanifest
toml
[features]
default = ["color"]
color = []
json = ["dep:serde_json"]

Cargo features enable or disable optional functionality.

Build with selected features

Enable one or more package features.

bashANYcargobuildfeatures
bash
cargo build --features json,color

Use a comma-separated feature list for the local package.

Disable default features

Build or test without defaults.

bashANYcargofeaturesdefaults
bash
cargo test --no-default-features

Useful to verify optional code paths compile independently.

Enable all features

Build everything gated by features.

bashANYcargofeaturesci
bash
cargo check --all-features

Helpful in CI for validating the full feature matrix.

Workspaces

Group crates into one workspace and operate across members.

Minimal workspace root

Define workspace members from a root manifest.

tomlANYcargoworkspacemanifest
toml
[workspace]
members = ["crates/*", "xtask"]
resolver = "2"

Workspaces let multiple packages share one lockfile and target directory.

Build every member

Compile all packages in the workspace.

bashANYcargoworkspacebuild
bash
cargo build --workspace

A common command at the monorepo root.

Test every member

Run tests across all workspace packages.

bashANYcargoworkspacetest
bash
cargo test --workspace

Helps enforce repository-wide quality gates.

Select one package in a workspace

Target a specific member crate.

bashANYcargoworkspacepackage
bash
cargo check -p xtask

Use `-p` or `--package` to focus on one workspace member.

Exclude a package

Skip a workspace member during a broad command.

bashANYcargoworkspaceexclude
bash
cargo test --workspace --exclude xtask

Useful when a member needs a different environment.

Common CLI crate recipes

Patterns often used in Rust command-line tools.

Use clap derive for argument parsing

Derive a parser struct for a CLI.

rustANYrustclapcli
rust
use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
  #[arg(long)]
  verbose: bool,

  #[arg(long, default_value = ".")]
  path: String,
}

The `clap` ecosystem is a common choice for Rust CLI argument parsing.

Return `Result` from main

Use anyhow for ergonomic CLI errors.

rustANYrustanyhowerrors
rust
fn main() -> anyhow::Result<()> {
  println!("hello");
  Ok(())
}

Returning `Result` from `main` is a clean pattern for CLI apps.

Set log level through env

Enable application logging with `RUST_LOG`.

bashANYrustloggingenv
bash
RUST_LOG=info cargo run

Many Rust apps use environment-controlled logging during development.

Recommended next

No recommendations yet.