我很难理解 Rust toml 中的功能条目。
假设我有一个依赖项(在本例中为 sqlx)说
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio", "macros", "json", "mysql" ] }
Rust 书中提到 Features :
Cargo supports features to allow expression of:
- conditional compilation options (usable through cfg attributes);
- optional dependencies, which enhance a package, but are not required; and
- clusters of optional dependencies, such as postgres-all, that would include the postgres package, the postgres-macros package, and possibly other packages (such as development-time mocking libraries, debugging tools, etc.).
A feature of a package is either an optional dependency, or a set of other features.
这对我来说意味着什么?我是否必须安装,即输入例如“runtime-tokio”作为我的 Cargo.toml 中的附加依赖项,或者 sqlx 是否已经附带“runtime-tokio”?
顺便说一句,cfg 属性是什么?
最佳答案
每当您为依赖项指定功能时,您都会要求 Cargo 以 crate 选择作为选项提供的方式以不同方式编译该依赖项。通常,但并非总是,该功能的命名与其可能依赖的 crate 相同(以提供例如特征实现)。
Do I have to install, i.e. enter e.g. "runtime-tokio" as an additional dependency in my Cargo.toml or does sqlx already come with "runtime-tokio"?
没有; sqlx
的 Cargo.toml 将负责为任意功能组合指定所需的依赖项。
what are cfg-attributes?
属性是写成#[attribute_name_here]
的东西,比如#[test]
和#[derive(Debug)]
。 The #[cfg(...)]
attribute允许条件编译,即告诉编译器假装源代码的某些部分不存在。
通常,一个功能有两种效果:
- 它启用可选的依赖项(这在
Cargo.toml
中指定,可以在 the[features]
section 中显式指定,也可以通过与 crate 依赖项同名的功能隐式指定)。 - 它会使用条件编译来编译(而不是忽略)使用该依赖项的包中的代码。
关于rust - Rust crate 功能需要单独安装吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64504415/