如何删除货物中未使用的依赖项?

发布于 2025-01-25 10:29:43 字数 33 浏览 1 评论 0原文

我如何找出未使用的货物中的依赖性?如何自动删除它们?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隐诗 2025-02-01 10:29:44

货物 - udeps需要夜间编译器,这可能引入潜在的不稳定性或兼容性问题。

我建议使用 machete 。它可以与稳定的生锈编译器一起使用,并消除了潜在的夜间建造风险。它提供了一种快速便捷的方式来识别潜在的未使用依赖性。同样,可以将其配置为忽略特定的依赖性或提供更详细的信息。

安装砍刀:

cargo install cargo-machete

在项目中查找未使用的依赖项:

cargo machete --with-metadata

修复未使用的依赖性:

cargo machete --fix
  --with-metadata   uses cargo-metadata to figure out the dependencies' names.
                    May be useful if some dependencies are renamed from their
                    own Cargo.toml file (e.g. xml-rs which gets renamed xml).
                    Try it if you get false positives!
  --skip-target-dir don't analyze anything contained in any target/ directories
                    encountered.
  --fix             rewrite the Cargo.toml files to automatically remove unused
                    dependencies. Note: all dependencies flagged by
                    cargo-machete will be removed, including false positives.
  --version         print version.

cargo-udeps requires the nightly compiler, which might introduce potential instability or compatibility issues.

I recommend to use machete. It works with the stable Rust compiler and eliminates the risk of potentially unstable nightly builds. It offers a fast and convenient way to identify potential unused dependencies. Also, it can be configured to ignore specific dependencies or provide more detailed information.

Install machete:

cargo install cargo-machete

Find unused dependencies in the project:

cargo machete --with-metadata

To fix unused dependencies:

cargo machete --fix
  --with-metadata   uses cargo-metadata to figure out the dependencies' names.
                    May be useful if some dependencies are renamed from their
                    own Cargo.toml file (e.g. xml-rs which gets renamed xml).
                    Try it if you get false positives!
  --skip-target-dir don't analyze anything contained in any target/ directories
                    encountered.
  --fix             rewrite the Cargo.toml files to automatically remove unused
                    dependencies. Note: all dependencies flagged by
                    cargo-machete will be removed, including false positives.
  --version         print version.

夜清冷一曲。 2025-02-01 10:29:44

cargo-shear 可以很好地使用,可以与稳定的工具链。

安装:

cargo install cargo-shear

检查是否有未使用的依赖项

cargo shear

可以修复(删除)未使用的依赖性:

cargo shear --fix

cargo-shear does this very nicely and can be used with the stable toolchain.

Install:

cargo install cargo-shear

Check if there are any unused dependencies

cargo shear

To fix (remove) an unused dependency:

cargo shear --fix
苏大泽ㄣ 2025-02-01 10:29:43

截至

2024年9月,此答案需要每晚运行。如果您不想夜间构建,请参考Michael Hall或Mujeeb Kalwar的答案。


一种选择是使用 cargo-udeps

您可以通过命令安装它:

cargo install cargo-udeps --locked

然后,要在生产目标中找到未使用的依赖项:

cargo +nightly udeps

找到未使用的dev依赖项:

cargo +nightly udeps --all-targets

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文