#cargo-subcommand #semver #rustdoc-json #diff

app cargo-public-api

List and diff the public API of Rust library crates between releases and commits. Detect breaking API changes and semver violations via CI or a CLI.

50 releases (26 breaking)

new 0.33.1 Nov 29, 2023
0.32.0 Aug 25, 2023
0.31.2 Jun 18, 2023
0.28.0 Mar 25, 2023
0.12.4 Jul 24, 2022

#1 in Cargo plugins

Download history 291/week @ 2023-08-10 364/week @ 2023-08-17 348/week @ 2023-08-24 313/week @ 2023-08-31 355/week @ 2023-09-07 124/week @ 2023-09-14 150/week @ 2023-09-21 131/week @ 2023-09-28 262/week @ 2023-10-05 288/week @ 2023-10-12 666/week @ 2023-10-19 341/week @ 2023-10-26 161/week @ 2023-11-02 288/week @ 2023-11-09 420/week @ 2023-11-16 219/week @ 2023-11-23

1,098 downloads per month

MIT license



List and diff the public API of Rust library crates between releases and commits. Detect breaking API changes and semver violations via CI or a CLI. Relies on and automatically builds rustdoc JSON, for which a recent version of the Rust nightly toolchain must be installed.


Install the cargo public-api subcommand with a recent regular stable Rust toolchain:

cargo +stable install cargo-public-api --locked

Ensure nightly-2023-08-25 or later is installed (does not need to be the active toolchain) so cargo public-api can build rustdoc JSON for you:

rustup install nightly --profile minimal


List the Public API

This example lists the public API of the regex crate. First we clone the repo:

git clone https://github.com/rust-lang/regex ; cd regex

Now we can list the public API of regex by running

cargo public-api

which will print the public API of regex with one line per public item in the API:

colored output of listing a public api

Diff the Public API

… Against a Specific Published Version

To diff the public API of the regex crate in the current directory against published version 1.6.0 on crates.io:

cargo public-api diff 1.6.0
colored output of diffing a public api

… Against the Latest Published Version

cargo public-api diff latest

… Between Git Commits

cargo public-api diff ref1..ref2

… as a CI Check

With a regular cargo test that you run in CI you will be able to

  • prevent accidental changes to your public API
  • review the public API diff of deliberate changes

First add the latest versions of the necessary libraries to your [dev-dependencies]:

cargo add --dev \
    rustup-toolchain \
    rustdoc-json \
    public-api \

Then add the following test to your project. As the author of the below test code, I hereby associate it with CC0 and to the extent possible under law waive all copyright and related or neighboring rights to it:

fn public_api() {
    // Install a compatible nightly toolchain if it is missing

    // Build rustdoc JSON
    let rustdoc_json = rustdoc_json::Builder::default()

    // Derive the public API from the rustdoc JSON
    let public_api = public_api::Builder::from_rustdoc_json(rustdoc_json)

    // Assert that the public API looks correct

Before you run the test the first time you need to bless the current public API:

UPDATE_EXPECT=1 cargo test public_api

This creates a tests/public-api.txt file in your project that you git add together with your other project files. Whenever you change the public API, you need to bless it again with the above command. If you forget to bless, the test will fail, together with instructions on how to bless.

Less Noisy Output

For completeness, items belonging to Blanket Implementations, Auto Trait Implementations, and Auto Derived Implementations, such as

  • impl<T, U> Into<U> for T where U: From<T>
  • impl Sync for ...
  • impl Debug for ... / #[derive(Debug)]

are included in the list of public items by default. Use

  • --omit blanket-impls
  • --omit auto-trait-impls
  • --omit auto-derived-impls

respectively to omit such items from the output to make it much less noisy:

cargo public-api --omit blanket-impls,auto-trait-impls,auto-derived-impls

For convenience you can also use -s (--simplified) to achieve the same thing. This is a shorter form of the above command:

cargo public-api -sss

Compatibility Matrix

cargo-public-api Understands the rustdoc JSON output of
0.32.x — 0.33.x nightly-2023-08-25 —
0.30.x — 0.31.x nightly-2023-05-24 — nightly-2023-08-24
0.26.x — 0.29.x nightly-2023-01-04 — nightly-2023-05-23
0.20.x — 0.25.x nightly-2022-09-28 — nightly-2023-01-03
0.19.x nightly-2022-09-08 — nightly-2022-09-27
earlier versions see here







~451K SLoC