It is now possible to show Rust dependencies using Cargo. Before Rust 1.44.0, there was not a built-in method to display a dependency graph. There was a plugin, though, called cargo-tree.
Show Rust Dependencies Using Cargo-tree
With the cargo-tree plugin, we need to install it as follows.
1 | cargo install cargo-tree |
Once we installed it, run the command cargo tree.
Let us say we have this Cargo.toml file.
1 2 3 4 5 6 7 8 | [package] name = "cargo-tree-demo" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] random = "0.12.2" |
The plugin outputs the following.
To remove the cargo-tree plugin, run the following command.
1 | cargo uninstall cargo-tree |
The command generates the following output.
1 | Removing C:\Users\kardev\.cargo\bin\cargo-tree.exe |
Now Cargo Built-In Command
Cargo-tree is now a Cargo build-in command starting with Rust 1.44.0. As such, all we need to do is update our Rust using the rustup update command. Alternatively, we could just install Rust 1.44.0 or later for new environments.
To ensure we are using the built-in command instead of the plugin, uninstall the cargo-tree plugin.
The same command works to show dependencies in Rust.
Although the same command works, some option flags are now different from that of the cargo-tree plugin. For example, --all-targets to now --no-filter-targets.
Some other option flags remain the same such as the -p or --package, which lists all dependencies of a crate. Let us say we have this updated Cargo.toml.
1 2 3 4 5 6 7 8 9 | [package] name = "cargo-tree-demo" version = "0.1.0" authors = ["Karl San Gabriel"] edition = "2018" [dependencies] random = "0.12.2" actix-rt = "1.1.1 |
To list all dependencies of actix-rt, we use the following command.
1 | cargo tree -p actix-rt |
To view the list of options flags, use the following command.
1 | cargo tree --help |