如何动态启用板条箱功能?

How to enable a crate feature dynamically?

我有一个箱子,可以编译有或没有功能,比方说 feat_crate

我从一个程序中使用该 crate,该程序也可以在有或没有功能的情况下编译 feat_app

我想在启用 feat_app 时为依赖项启用 feat_cratefeat_app 在构建应用程序时启用(如 cargo run -- --feat_app ⚠ 编辑,如 cargo run --features feat_app) .

每次我想更改已启用的功能时,如果不修改 Cargo.toml 文件,我找不到一种简单的方法来做到这一点。我试着查看构建脚本,但应用程序的脚本是在编译依赖项后执行的,所以它似乎没有帮助。

我可能可以使用从 crate 构建脚本中获取的环境变量,这意味着我必须相应地设置该环境变量……但我希望有更好的解决方案。

来自the features documentation

Features can be used to reexport features of other packages. The session feature of package awesome (nb: that's the "current" package) will ensure that the session feature of the package cookie is also enabled.

session = ["cookie/session"]