如何定义仅测试依赖项?

How to define test-only dependencies?

我有一个实现了 lint 插件的 Rust 库。我想包括 compiletest,但在测试之外不需要它。指定依赖项仅用于测试的正确方法是什么?

是的。使用 dev-dependencies。来自 Cargo docs:

You can add a [dev-dependencies] section to your Cargo.toml whose format is equivalent to [dependencies]:

[dev-dependencies]
tempdir = "0.3"

Dev-dependencies are not used when compiling a package for building, but are used for compiling tests, examples, and benchmarks.

如果可能,您还应该使用 Cargo 的 resolver version 2 来更好地处理复杂的开发依赖案例。