Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Tools

cargo tree and cargo metadata

Reference for cargo tree and cargo metadata: print the dependency graph, find duplicates, see why a crate is included and export machine-readable JSON.

cargo tree prints your package's dependency graph as an indented tree, built from the manifests and Cargo.lock. It answers questions such as "why is this crate in my build?", "which versions of syn do I compile?" and "what does enabling this feature pull in?". cargo metadata prints the same information (and more) as JSON for tools and scripts.

Both commands read the resolved graph; neither compiles anything. This page is a read-only command reference, and the examples model how a tree is drawn and how duplicates are spotted.

Terminal
$ cargo tree
$ cargo tree -i syn
$ cargo tree -d

Try it

Rust Drawing a dependency tree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Finding duplicate versions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Terminal
cargo tree [options]
cargo metadata [options]
Name Description
-i, --invert <spec> Show what depends on the given package (the reverse tree).
-d, --duplicates List packages that appear with more than one version.
-e, --edges Choose edge kinds: normal, build, dev, features, all.
-f, --format Customize each line, for example "{p} {f}" to show features.
--depth n Limit how deep the tree goes.
--prefix indent (default), depth or none for flat output.
--no-dedupe Print repeated subtrees in full instead of (*).
--format-version 1 (cargo metadata) Select the JSON schema version; always pass it.
--no-deps (cargo metadata) Only describe workspace members.

Usage notes

  • A (*) after a package means its dependencies were already printed higher up and are not repeated.
  • cargo tree -i foo -e features shows which feature of which crate turned foo on, the fastest way to debug feature creep.
  • Multiple versions of one crate are legal and common after major releases; too many slow builds and bloat binaries.
  • Platform-specific dependencies are shown for the host unless you pass --target.
  • cargo metadata --format-version 1 is the supported way for editors and build tools to learn about a workspace; never parse Cargo.toml yourself.
  • The output reflects Cargo.lock; run cargo update first if you want to see newer versions.
  • cargo tree was merged into Cargo in 1.44 (it began as the cargo-tree crate).