rust - Rust crate 功能需要单独安装吗?

标签 rust rust-cargo

我很难理解 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/

相关文章:

powershell - 显示颜色代码而不是颜色

rust - 什么是解决 future 需要多长时间的干净方法?

rust - 模式匹配错误 "type of this value must be known in this context"

linux - 如何在 RHEL Linux 服务器上安装 Cargo?

rust - 这是什么意思? "This is precisely because a library should not be deterministically recompiled for all users of the library."

rust - `cargo package` 错误 : extern location for foo does not exist

rust - 如何将 Box<T> 转换为 &T?

rust - 为什么 for_each 在 Release模式下(cargo run -r)比 for 循环快得多?

rust - 使用actix-files提供静态文件时,如何设置 “expire”或 “Cache-Control” header ?

rust - 如何解决 rust 中的 “value: ParseIntError”?