如何发布具有可选依赖项的板条箱?

How to publish a crate with optional dependency?

我在发布带有可选依赖项的 crate 时遇到了一些麻烦。

首先,我执行 cargo publish,但我没有看到编译的任何可选依赖项。

然后,我运行cargo publish --all-features。依赖项已编译,但我在 crates.io.

生成的文档中没有看到任何具有可选依赖项的模块

发布带有可选依赖项的 crate 的正确方法是什么,它不是 Cargo.toml 中的默认功能设置?

cargo publish 是正确的做法。

如果您希望 docs.rs(而不是 crates.io)在启用某些功能的情况下构建您的文档,请使用 Cargo.toml 中的 package.metadata.docs.rs 部分。我以 Cargo.toml from the petgraph crate 为例(剥离):

[package]
name = "petgraph"
version = "0.6.0"
description = "Graph data structure library. Provides graph types and graph algorithms."
edition = "2018"

[dependencies]
fixedbitset = { version = "0.4.0", default-features = false }
indexmap = { version = "1.6.2" }
quickcheck = { optional = true, version = "0.8", default-features = false }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }

[...]

[features]
default = ["graphmap", "stable_graph", "matrix_graph"]
graphmap = []
stable_graph = []
matrix_graph = []
serde-1 = ["serde", "serde_derive"]

[package.metadata.docs.rs]
features = ["serde-1", "quickcheck"]