你如何启用 Rust 的“板条箱功能”?

bru*_*olf 24 rust rust-cargo

我正在尝试使用rand::SmallRng. 文档说

此 PRNG 是功能门控的:要使用,您必须启用 crate 功能small_rng

我一直在搜索,但不知道如何启用“板条箱功能”。Rust 文档中的任何地方都没有使用该短语。这是我能想到的最好的:

[features]
default = ["small_rng"]
Run Code Online (Sandbox Code Playgroud)

但我得到:

功能default包括small_rng既不是依赖项也不是其他功能的

文档有误,还是我遗漏了什么?

edw*_*rdw 25

在 Cargo.toml 中指定依赖项,如下所示:

[dependencies]
rand = { version = "0.7.2", features = ["small_rng"] }
Run Code Online (Sandbox Code Playgroud)

或者:

[dependencies.rand]
version = "0.7.2"
features = ["small_rng"]
Run Code Online (Sandbox Code Playgroud)

两者都有效。


dhi*_*aka 8

添加您可以使用的功能。即使您最初添加了 crate 并且想要稍后添加功能,这也适用

cargo add rand -F small_rng
Run Code Online (Sandbox Code Playgroud)