Optional Rust migration track · Lesson 03 · About 15 minutes after setup
Enforce a Rust dependency constraint
Keep a forbidden dependency out of a Rust component, even when its code compiles.
Choose the unit your rule governs
A Rust crate is a compilation unit, such as a library or executable. A Cargo
package is the project described by one Cargo.toml; it can contain multiple
crate targets. A module organizes items inside a crate. Cargo dependency tools inspect
package relationships, while a rule about two modules inside one crate needs a different view.
Packages and crates.
| Requirement | Tool to investigate |
|---|---|
| This package must not depend on a particular library. | cargo-deny: package dependency policy. |
| Inspect internal dependencies and reject cycles in the selected graph. | cargo-modules: structure, dependency graphs, and an acyclic check. |
| A compatible library release must preserve its public Rust API. | cargo-semver-checks: compare the API with an earlier version. |
| Only selected modules should access an implementation detail. | Rust visibility: private items and restricted pub visibility checked by the compiler. |
These complement ArchUnit on the Java side. Today's exercise uses cargo-deny because
the constraint concerns a dependency declared in a manifest. Read its
deny field documentation.
Set up an isolated experiment
Use Cargo and a Rust toolchain supported by your cargo-deny release. The course verified cargo-deny 0.22.1 with Rust 1.98.0. Installing the pinned tool is a one-time step and may take several minutes:
cargo install --locked cargo-deny --version 0.22.1
cargo deny --version
For this session, a verified temporary binary is also available; see the Rust setup note. Create your own exercise package:
cd "/home/dmedeiros/Documents/ChatGPT/Teach me ArchUnit"
cargo new --bin --vcs none exercises/rust-dependency-lab
cd exercises/rust-dependency-lab
cargo add regex@1
cargo check
Leave the generated Hello World program unchanged. Cargo should accept the declared dependency
even though the program never calls it. This experiment chooses regex arbitrarily;
the ban represents a team policy, not a problem with the library.
cargo add.
Exercise: write the policy yourself
Create deny.toml from scratch beside this package's Cargo.toml.
Write a minimal policy that bans every version of regex and gives a short reason.
Use the official [bans] documentation to find the syntax. Keep the program unchanged.
- Run
cargo deny check bans. Expect a nonzero exit and anerror[banned]diagnostic namingregex. A tool installation or network error does not demonstrate the rule. - Run
cargo remove regex, then repeat the check. Expect success; keep the ban in place. - Run
cargo add regex@1. The same policy must reject the dependency again. - Remove the dependency once more and finish with
cargo checkandcargo deny check banspassing.
Configuration hint
The relevant table is [bans].
Its deny field is a list; entries can carry a package name and a
reason. You do not need a full generated configuration to express this one policy.
Explain why this check fails even when no Rust source mentions the library, while ArchUnit's unused Java import experiment may produce no dependency. The two tools inspect different evidence.
What about existing Rust dependency debt?
cargo-deny supports explicit wrappers for a banned dependency: named packages can
depend directly on it while other consumers remain forbidden. A permitted wrapper keeps its permission
until you remove it. This is a scoped exception, without ArchUnit's automatic shrinking set of
violation descriptions. See the wrapper diagnostics.
Later practice will add an approved adapter and an unapproved consumer, then inspect the graph with cargo-modules and test a Rust API change with cargo-semver-checks. The migration reference also covers experimental Rust architecture libraries and Java/Rust service contracts.