Rust projects often start with a small number of dependencies. As development continues, developers add crates for serialization, logging, database access, networking, testing, and other features. Over time, some of these dependencies may no longer be needed.
A feature may be removed, an implementation may be replaced, or a standard library feature may make an external crate unnecessary. However, the dependency can remain in Cargo.toml even after the application stops using it.
Unused dependencies are easy to overlook because an application can still compile and run successfully with them. They can also increase build time, add unnecessary packages to the dependency graph, and make dependency maintenance more complicated.
Cargo now provides a built-in unused dependency lint that can identify many dependencies that are not used by a package's targets. Developers can also use tools such as cargo-machete and cargo-udeps for additional analysis.
This article explains how to find unused dependencies in Rust projects, enable Cargo's native lint, analyze workspace dependencies, and safely remove unnecessary crates.
Why Unused Dependencies Matter
A dependency is not free simply because it appears in Cargo.toml.
When a project declares an external crate, Cargo includes it in dependency resolution. Depending on how the crate is used, it may also be compiled and linked into the application.
An unused dependency can therefore introduce unnecessary work into the development process.
Longer Build Times
Some crates have large dependency trees. Keeping an unnecessary direct dependency can cause Cargo to resolve and compile additional packages that the application no longer needs.
The effect is especially noticeable in projects with many dependencies or large workspaces.
Removing unused crates can reduce the work required for full builds and linking. The improvement depends on the dependency graph and whether other crates still require the same packages.
Larger Dependency Graphs
Every direct dependency becomes part of the project's dependency configuration. It may introduce transitive dependencies, version constraints, features, and platform-specific requirements.
An unused crate can make this configuration more complicated without providing any value.
Increased Maintenance Work
Dependencies need to be reviewed, updated, and checked for compatibility.
Keeping unnecessary crates means developers may spend time reviewing updates for packages that the application no longer needs.
Removing them makes the dependency configuration easier to understand.
Additional Supply Chain Exposure
Every external package introduces code maintained outside the project.
An unnecessary dependency increases the number of packages developers need to review for security advisories, licensing requirements, and maintenance activity.
Removing unused dependencies helps reduce this unnecessary exposure.
Using Cargo's Built-In unused_dependencies Lint
Cargo now includes a native lint named cargo::unused_dependencies.
It checks whether dependencies declared in Cargo.toml are used by the selected Cargo targets.
The lint is part of Cargo's own diagnostic system rather than the Rust compiler's ordinary code lints.
It is enabled at the warn level by default in current Cargo documentation. Developers can also configure its level explicitly in the manifest.
For example, consider a project with the following dependencies:
[package]
name = "inventory-service"
version = "0.1.0"
edition = "2024"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
regex = "1"
Suppose the application uses serde and serde_json but no longer uses regex.
Cargo can identify regex as a dependency that is not used by the selected targets.
Run the following command:
cargo check
The unused dependency lint can report the unnecessary entry during the check.
The exact diagnostic depends on the Cargo version, selected targets, and dependency configuration.
Configuring the Lint Explicitly
Although the lint is enabled by default as a warning, you can configure it in Cargo.toml.
[lints.cargo]
unused_dependencies = "warn"
This makes the intention clear to developers working on the project.
You can also change the warning into an error:
[lints.cargo]
unused_dependencies = "deny"
With deny, Cargo treats detected unused dependencies as errors.
This is useful when a team wants to prevent new unused dependencies from entering the project.
For an existing codebase, starting with warn is usually more practical. It allows developers to review the findings before making dependency cleanup part of the build requirements.
Checking All Targets in a Rust Project
A Rust package may contain more than one target.
For example, it may include:
A dependency used by one target may appear unused when Cargo checks another target.
For example, a crate may be used only by an integration test. Checking only the main application target may not provide the complete picture.
Use the following command to check all selected targets in the package:
cargo check --all-targets
For a workspace containing multiple packages, use:
cargo check --workspace --all-targets
This gives Cargo a broader view of the workspace and its targets.
However, there is an important limitation: the native unused_dependencies lint does not currently check every dependency category in every situation.
In particular, Cargo's documentation notes that there is no way yet to select all targets that use dev-dependencies for this lint. As a result, dependencies declared under dev-dependencies may not be covered by the native check.
A successful cargo check --workspace --all-targets should therefore not be treated as proof that every development dependency is used.
Finding Unused Dependencies in a Workspace
Large Rust applications often use Cargo workspaces to organize multiple related crates.
A workspace may contain separate packages for:
The root Cargo.toml may define shared dependencies that individual packages inherit.
For example:
[workspace]
members = [
"api",
"database",
"shared"
]
[workspace.dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
An individual package can inherit a dependency from the workspace:
[dependencies]
serde.workspace = true
tokio.workspace = true
This approach keeps dependency versions consistent across packages.
However, a workspace dependency may remain in the root manifest even after every package stops inheriting it.
Cargo provides another lint for this situation: cargo::unused_workspace_dependencies.
Detecting Unused Workspace Dependencies
The unused_workspace_dependencies lint checks whether entries in workspace.dependencies are inherited by workspace packages.
For example, if serde_json remains in the workspace manifest but no package inherits it, Cargo can report that entry as unused.
You can configure both lints in the workspace manifest:
[workspace.lints.cargo]
unused_dependencies = "warn"
unused_workspace_dependencies = "warn"
Workspace lint settings are not automatically inherited by every member package. Each package that should use the workspace settings needs to opt into inheritance.
For example, in api/Cargo.toml:
[package]
name = "api"
version = "0.1.0"
edition = "2024"
[lints]
workspace = true
The same configuration can be added to other workspace members.
This makes it easier to apply consistent Cargo lint settings across a multi-package project.
Using cargo-machete to Find Unused Dependencies
Cargo's native lint is useful, but it does not cover every dependency usage pattern.
A popular alternative is cargo-machete.
It is a third-party Cargo tool that scans Rust projects and identifies dependencies declared in Cargo.toml that do not appear to be used in the source code.
Installing cargo-machete
Install the tool using Cargo:
cargo install cargo-machete
After installation, run it from the project directory:
cargo machete
It can also scan a directory containing multiple Rust projects:
cargo machete ./my-rust-workspace
The tool searches for Cargo.toml files and analyzes their dependencies.
Understanding the Output
Suppose a project contains these dependencies:
[dependencies]
serde = "1"
serde_json = "1"
regex = "1"
log = "0.4"
If the source code uses serde and serde_json but does not reference regex or log, cargo-machete may report them as unused.
The output identifies the package and the dependency candidates that should be reviewed.
The tool uses heuristic analysis, so its findings are not always correct. A dependency may be used indirectly through generated code, macros, or build scripts in ways that the scanner cannot identify.
Do not remove every reported dependency without reviewing its actual purpose.
Ignoring Dependencies That Are Used Indirectly
Sometimes a dependency is required even though its name does not appear in the ordinary source files.
For example, a build script may generate Rust code that references a crate.
You can configure cargo-machete to ignore a dependency:
[package.metadata.cargo-machete]
ignored = [
"prost"
]
For a workspace-wide configuration, use:
[workspace.metadata.cargo-machete]
ignored = [
"prost"
]
This tells cargo-machete that the dependency should not be reported as unused.
Use this setting only when there is a clear reason to retain the dependency.
Handling Renamed Dependencies
Cargo allows developers to rename dependencies in Cargo.toml.
For example:
[dependencies]
web-framework = {
package = "actual-framework",
version = "1"
}
The package name and the name used in Rust source code are different.
A heuristic scanner may not automatically recognize this relationship.
cargo-machete supports a renamed configuration:
[package.metadata.cargo-machete.renamed]
web-framework = "actual-framework"
This helps the tool understand the relationship between the manifest name and the crate name used in source code.
Using cargo-udeps for Deeper Analysis
Another option is cargo-udeps.
It identifies dependencies declared in Cargo.toml that are not used by the project.
Unlike cargo-machete's source-code heuristics, cargo-udeps uses compiler and Cargo information to perform its analysis.
This can help identify cases that are difficult for simple text-based scanning.
Installing cargo-udeps
Install the tool:
cargo install cargo-udeps --locked
Although it can be compiled using stable Rust, running cargo-udeps requires the nightly toolchain.
Install nightly if it is not already available:
rustup toolchain install nightly
Run the analysis:
cargo +nightly udeps
For a workspace:
cargo +nightly udeps --workspace
The tool reports unused dependencies across the selected packages.
Ignoring Dependencies
Some dependencies are intentionally used in situations that a tool cannot fully analyze.
cargo-udeps supports an ignore configuration in Cargo.toml:
[package.metadata.cargo-udeps.ignore]
normal = [
"prost"
]
The metadata can also be configured at the workspace level.
This is useful when a dependency is required by generated code or another build process that the analysis cannot inspect completely.
Comparing Cargo's Native Lint With External Tools
The available tools have different approaches and limitations.
Tool | How It Works | Main Advantage | Limitation |
|---|
cargo::unused_dependencies | Cargo-native lint | No additional tool installation | Does not cover every dependency category or target |
cargo-machete | Heuristic source analysis | Fast and easy to run | Can produce false positives |
cargo-udeps | Compiler-based analysis | Useful for deeper dependency checks | Requires nightly Rust to run |
Cargo's own documentation recommends periodically reviewing unused dependencies and mentions cargo-machete, cargo-udeps, and cargo-shear as useful external tools.
For most projects, starting with Cargo's native lint is convenient. Adding cargo-machete or cargo-udeps can provide another perspective when the project has complicated dependency usage.
Understanding Common False Positives
No unused dependency checker can understand every possible way a Rust project uses a crate.
A dependency may appear unused for several reasons.
Dependencies Used by Generated Code
Some projects generate Rust source files during the build process.
For example, a build script may generate code that references a serialization crate.
A scanner that examines only existing source files may not recognize this usage.
Before removing the dependency, inspect build.rs and the generated source configuration.
Dependencies Used by Procedural Macros
Procedural macros and derive macros can introduce code that is not immediately obvious from ordinary source references.
Check macro definitions and generated code before removing a dependency that appears unused.
Dependencies Used for Feature Activation
Cargo dependencies can enable features on other crates.
For example:
[dependencies]
serde = "1"
some-library = {
version = "2",
features = ["serialization"]
}
A dependency may be present to control the feature configuration of another crate rather than because the application imports it directly.
Cargo's native lint documentation specifically warns that dependencies used to activate features on transitive dependencies can produce false positives.
Review the dependency graph and feature configuration before removing such entries.
Dependencies Used Only on Specific Platforms
Rust supports target-specific dependencies.
For example:
[target.'cfg(windows)'.dependencies]
windows-sys = "0.59"
A dependency may be required only when compiling for Windows.
When checking a project, consider all target platforms supported by the application. A dependency that appears unnecessary on Linux may still be essential on Windows or another target.
Removing an Unused Dependency Safely
Once a dependency has been confirmed as unnecessary, remove it from Cargo.toml.
For example, consider this manifest:
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
regex = "1"
If the application no longer uses regex, remove that entry:
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Then run:
cargo check
Run the test suite:
cargo test --workspace
If the project includes examples, integration tests, or benchmarks, test those targets as well.
Finally, inspect the changes to Cargo.lock.
Cargo updates the lockfile as part of dependency resolution. Review the changes to ensure that only the expected packages and versions were affected.
Do not manually delete arbitrary entries from Cargo.lock. Let Cargo maintain the dependency resolution.
Adding Unused Dependency Checks to CI/CD
Finding unused dependencies once is useful, but checking them regularly helps prevent the problem from returning.
You can configure Cargo's native lint to treat unused dependencies as errors:
[lints.cargo]
unused_dependencies = "deny"
Then run a workspace check in your CI pipeline:
cargo check --workspace --all-targets
This makes the check part of the normal validation process.
You can also add cargo-machete as a separate CI step:
name: Rust Dependency Check
on:
pull_request:
push:
jobs:
dependencies:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Check unused dependencies
run: |
cargo install cargo-machete
cargo machete
For a production CI pipeline, consider installing a pinned cargo-machete version rather than relying on whichever version is latest at execution time.
The native lint and the external scanner complement each other. The native lint runs as part of Cargo's checks, while cargo-machete provides a separate analysis pass.
Remember that neither approach guarantees that every dependency is unnecessary or necessary. Review findings before making changes.
Common Mistakes When Removing Dependencies
Removing a Dependency Without Checking Build Scripts
A dependency may be used by build.rs or generated code.
Check the build process before removing it.
Ignoring Development Dependencies
A crate may be required by integration tests, examples, or test utilities even when it is not used by the main application.
Review the dev-dependencies section separately, particularly because Cargo's native lint has limitations in this area.
Removing Platform-Specific Dependencies
A dependency may be needed only on one operating system or target architecture.
Test the platforms supported by the project before removing it.
Assuming a Clean Build Proves Everything Is Correct
A successful cargo check does not guarantee that every runtime feature still works.
Run tests and verify application behavior after removing dependencies.
Treating Tool Warnings as Definitive
Static analysis tools can produce false positives.
Check whether a dependency is used indirectly, through generated code, macros, build scripts, or feature activation.
Best Practices for Managing Rust Dependencies
A clean dependency configuration is easier to maintain and understand.
Consider the following practices:
Review Cargo.toml whenever a feature or module is removed.
Run Cargo's unused dependency lint regularly.
Use cargo check --workspace --all-targets for broader workspace checks.
Review dev-dependencies separately.
Use cargo-machete for additional dependency analysis.
Use cargo-udeps when deeper compiler-based analysis is useful.
Document dependencies that are intentionally retained for indirect usage.
Check target-specific dependencies before removing them.
Review Cargo.lock changes after dependency cleanup.
Run the full test suite after removing crates.
Add dependency checks to CI/CD pipelines.
Avoid automatically deleting dependencies based only on a scanner's output.
It is also useful to review dependency features separately. A crate may still be required while some of its enabled features are unnecessary. Tools such as cargo-features-manager and cargo-unused-features can help investigate that separate problem.
Advantages and Limitations
Finding unused dependencies provides several benefits for Rust projects.
It can reduce unnecessary compilation work, simplify Cargo.toml, remove unwanted transitive dependencies, and make dependency updates easier to manage. It can also reduce the number of external packages that need security and license reviews.
However, dependency analysis has limitations. Some crates are used indirectly through generated code, macros, build scripts, or feature activation. Target-specific and development dependencies also require careful review.
Cargo's native lint is convenient because it is integrated into the normal build workflow, while external tools provide additional analysis techniques.
The best results come from combining automated checks with developer review and application testing.
Summary
Unused dependencies can remain in Rust projects long after the code that required them has been removed. Although they may not immediately break an application, they can increase build work, complicate dependency management, and introduce unnecessary maintenance responsibilities.
Cargo's built-in unused_dependencies lint provides a convenient way to identify many unused dependencies during normal checks. The unused_workspace_dependencies lint helps identify workspace dependencies that are declared but never inherited.
For additional analysis, developers can use cargo-machete and cargo-udeps. Each tool has different strengths and limitations, so findings should be reviewed before removing crates.
By including dependency checks in regular development and CI/CD workflows, Rust teams can keep their Cargo manifests cleaner and make projects easier to build, maintain, and secure.
Join the conversation! Your thoughts help the community grow.