rustrust-cargo

How do I remove unused dependencies in Cargo.toml?


How do I find out the dependencies in Cargo.toml that are unused? How can I remove them automatically?


Solution

  • Update for 2024

    As of Sep 2024, this answer requires nightly to run. If you don't want nightly builds, refer to Michael Hall's or Mujeeb Kalwar's answer instead.


    One option is to use cargo-udeps.

    You can install it via command:

    cargo install cargo-udeps --locked
    

    Then, to find unused dependencies in production target:

    cargo +nightly udeps
    

    To find unused dev dependencies:

    cargo +nightly udeps --all-targets