如何判断每个板条箱有哪些“功能”可用?

stu*_*art 10 rust rust-crates rust-cargo

是否有一种标准方法可以确定给定板条箱可用的功能?

我正在尝试阅读 Postgres 时区,表示要使用 cratepostgres = "0.17.0-alpha.1"板条箱with-timewith-chrono功能。

当我在 Cargo.toml 中尝试此操作时:

[dependencies]
postgres = { version = "0.17.0-alpha.1", features = ["with-time"] }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

[dependencies]
postgres = { version = "0.17.0-alpha.1", features = ["with-time"] }
Run Code Online (Sandbox Code Playgroud)

此外,postgres 0.17.0crate 页面没有说明这些功能,所以我什至不知道是否应该支持它们。

似乎docs.rs 上会有一些关于它的内容?

She*_*ter 15

查看可用功能的唯一保证方法是查看 Cargo.toml 以获取 crate。这通常意味着您需要导航到项目的存储库,找到您感兴趣的版本的正确文件,然后阅读它。

您主要查找该[features]部分,但也查找标记为 的任何依赖项optional = true,因为可选依赖项计为隐式功能标志

不幸的是,没有要求板条箱作者提供关于每个功能标志的作用的任何文档。好的 crate 会在一个或多个位置记录它们的特征标志:

  • 正如 Cargo.toml 中的评论
  • 他们的自述文件
  • 他们的文档

除了查看 Cargo.toml 之外,上传到 docs.rs 的 crate 将显示存在哪些功能标志

crates.io issue #465建议将功能列表也放在 crate 的页面上。

也可以看看:


对于 postgres crate,我们可以从 crates.io 开始,然后点击“repository”去到repository。然后我们找到正确的标签 ( postgres-v0.17.0),然后读取Cargo.toml

[features]
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
with-geo-types-0_4 = ["tokio-postgres/with-geo-types-0_4"]
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
Run Code Online (Sandbox Code Playgroud)


Ake*_*ian 15

您可以使用cargo-feature crate来查看和管理依赖项的功能:

> cargo install cargo-feature --locked
Run Code Online (Sandbox Code Playgroud)
> cargo feature postgres
   Avaliable features for `postgres`
array-impls = ["tokio-postgres/array-impls"]
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
with-eui48-1 = ["tokio-postgres/with-eui48-1"]
with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"]
with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
with-smol_str-01 = ["tokio-postgres/with-smol_str-01"]
with-time-0_2 = ["tokio-postgres/with-time-0_2"]
with-time-0_3 = ["tokio-postgres/with-time-0_3"]
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
with-uuid-1 = ["tokio-postgres/with-uuid-1"]
Run Code Online (Sandbox Code Playgroud)

注意:只有当板条箱已经Cargo.toml作为依赖项存在时,这才有效。

使用时也会显示功能列表cargo add

> cargo add postgres
    Updating crates.io index
      Adding postgres v0.19.4 to dependencies.
             Features:
             - array-impls
             - with-bit-vec-0_6
             - with-chrono-0_4
             - with-eui48-0_4
             - with-eui48-1
             - with-geo-types-0_6
             - with-geo-types-0_7
             - with-serde_json-1
             - with-smol_str-01
             - with-time-0_2
             - with-time-0_3
             - with-uuid-0_8
             - with-uuid-1
Run Code Online (Sandbox Code Playgroud)